code.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.validateUnion = exports.validateArray = exports.usePattern = exports.callValidateCode = exports.schemaProperties = exports.allSchemaProperties = exports.noPropertyInData = exports.propertyInData = exports.isOwnProperty = exports.hasPropFunc = exports.reportMissingProp = exports.checkMissingProp = exports.checkReportMissingProp = void 0;
  4. const codegen_1 = require("../compile/codegen");
  5. const util_1 = require("../compile/util");
  6. const names_1 = require("../compile/names");
  7. const util_2 = require("../compile/util");
  8. function checkReportMissingProp(cxt, prop) {
  9. const { gen, data, it } = cxt;
  10. gen.if(noPropertyInData(gen, data, prop, it.opts.ownProperties), () => {
  11. cxt.setParams({ missingProperty: (0, codegen_1._) `${prop}` }, true);
  12. cxt.error();
  13. });
  14. }
  15. exports.checkReportMissingProp = checkReportMissingProp;
  16. function checkMissingProp({ gen, data, it: { opts } }, properties, missing) {
  17. return (0, codegen_1.or)(...properties.map((prop) => (0, codegen_1.and)(noPropertyInData(gen, data, prop, opts.ownProperties), (0, codegen_1._) `${missing} = ${prop}`)));
  18. }
  19. exports.checkMissingProp = checkMissingProp;
  20. function reportMissingProp(cxt, missing) {
  21. cxt.setParams({ missingProperty: missing }, true);
  22. cxt.error();
  23. }
  24. exports.reportMissingProp = reportMissingProp;
  25. function hasPropFunc(gen) {
  26. return gen.scopeValue("func", {
  27. // eslint-disable-next-line @typescript-eslint/unbound-method
  28. ref: Object.prototype.hasOwnProperty,
  29. code: (0, codegen_1._) `Object.prototype.hasOwnProperty`,
  30. });
  31. }
  32. exports.hasPropFunc = hasPropFunc;
  33. function isOwnProperty(gen, data, property) {
  34. return (0, codegen_1._) `${hasPropFunc(gen)}.call(${data}, ${property})`;
  35. }
  36. exports.isOwnProperty = isOwnProperty;
  37. function propertyInData(gen, data, property, ownProperties) {
  38. const cond = (0, codegen_1._) `${data}${(0, codegen_1.getProperty)(property)} !== undefined`;
  39. return ownProperties ? (0, codegen_1._) `${cond} && ${isOwnProperty(gen, data, property)}` : cond;
  40. }
  41. exports.propertyInData = propertyInData;
  42. function noPropertyInData(gen, data, property, ownProperties) {
  43. const cond = (0, codegen_1._) `${data}${(0, codegen_1.getProperty)(property)} === undefined`;
  44. return ownProperties ? (0, codegen_1.or)(cond, (0, codegen_1.not)(isOwnProperty(gen, data, property))) : cond;
  45. }
  46. exports.noPropertyInData = noPropertyInData;
  47. function allSchemaProperties(schemaMap) {
  48. return schemaMap ? Object.keys(schemaMap).filter((p) => p !== "__proto__") : [];
  49. }
  50. exports.allSchemaProperties = allSchemaProperties;
  51. function schemaProperties(it, schemaMap) {
  52. return allSchemaProperties(schemaMap).filter((p) => !(0, util_1.alwaysValidSchema)(it, schemaMap[p]));
  53. }
  54. exports.schemaProperties = schemaProperties;
  55. function callValidateCode({ schemaCode, data, it: { gen, topSchemaRef, schemaPath, errorPath }, it }, func, context, passSchema) {
  56. const dataAndSchema = passSchema ? (0, codegen_1._) `${schemaCode}, ${data}, ${topSchemaRef}${schemaPath}` : data;
  57. const valCxt = [
  58. [names_1.default.instancePath, (0, codegen_1.strConcat)(names_1.default.instancePath, errorPath)],
  59. [names_1.default.parentData, it.parentData],
  60. [names_1.default.parentDataProperty, it.parentDataProperty],
  61. [names_1.default.rootData, names_1.default.rootData],
  62. ];
  63. if (it.opts.dynamicRef)
  64. valCxt.push([names_1.default.dynamicAnchors, names_1.default.dynamicAnchors]);
  65. const args = (0, codegen_1._) `${dataAndSchema}, ${gen.object(...valCxt)}`;
  66. return context !== codegen_1.nil ? (0, codegen_1._) `${func}.call(${context}, ${args})` : (0, codegen_1._) `${func}(${args})`;
  67. }
  68. exports.callValidateCode = callValidateCode;
  69. const newRegExp = (0, codegen_1._) `new RegExp`;
  70. function usePattern({ gen, it: { opts } }, pattern) {
  71. const u = opts.unicodeRegExp ? "u" : "";
  72. const { regExp } = opts.code;
  73. const rx = regExp(pattern, u);
  74. return gen.scopeValue("pattern", {
  75. key: rx.toString(),
  76. ref: rx,
  77. code: (0, codegen_1._) `${regExp.code === "new RegExp" ? newRegExp : (0, util_2.useFunc)(gen, regExp)}(${pattern}, ${u})`,
  78. });
  79. }
  80. exports.usePattern = usePattern;
  81. function validateArray(cxt) {
  82. const { gen, data, keyword, it } = cxt;
  83. const valid = gen.name("valid");
  84. if (it.allErrors) {
  85. const validArr = gen.let("valid", true);
  86. validateItems(() => gen.assign(validArr, false));
  87. return validArr;
  88. }
  89. gen.var(valid, true);
  90. validateItems(() => gen.break());
  91. return valid;
  92. function validateItems(notValid) {
  93. const len = gen.const("len", (0, codegen_1._) `${data}.length`);
  94. gen.forRange("i", 0, len, (i) => {
  95. cxt.subschema({
  96. keyword,
  97. dataProp: i,
  98. dataPropType: util_1.Type.Num,
  99. }, valid);
  100. gen.if((0, codegen_1.not)(valid), notValid);
  101. });
  102. }
  103. }
  104. exports.validateArray = validateArray;
  105. function validateUnion(cxt) {
  106. const { gen, schema, keyword, it } = cxt;
  107. /* istanbul ignore if */
  108. if (!Array.isArray(schema))
  109. throw new Error("ajv implementation error");
  110. const alwaysValid = schema.some((sch) => (0, util_1.alwaysValidSchema)(it, sch));
  111. if (alwaysValid && !it.opts.unevaluated)
  112. return;
  113. const valid = gen.let("valid", false);
  114. const schValid = gen.name("_valid");
  115. gen.block(() => schema.forEach((_sch, i) => {
  116. const schCxt = cxt.subschema({
  117. keyword,
  118. schemaProp: i,
  119. compositeRule: true,
  120. }, schValid);
  121. gen.assign(valid, (0, codegen_1._) `${valid} || ${schValid}`);
  122. const merged = cxt.mergeValidEvaluated(schCxt, schValid);
  123. // can short-circuit if `unevaluatedProperties/Items` not supported (opts.unevaluated !== true)
  124. // or if all properties and items were evaluated (it.props === true && it.items === true)
  125. if (!merged)
  126. gen.if((0, codegen_1.not)(valid));
  127. }));
  128. cxt.result(valid, () => cxt.reset(), () => cxt.error(true));
  129. }
  130. exports.validateUnion = validateUnion;
  131. //# sourceMappingURL=code.js.map