subschema.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.applySubschema = exports.Type = void 0;
  4. const validate_1 = require("./validate");
  5. const util_1 = require("./util");
  6. const codegen_1 = require("./codegen");
  7. var Type;
  8. (function (Type) {
  9. Type[Type["Num"] = 0] = "Num";
  10. Type[Type["Str"] = 1] = "Str";
  11. })(Type = exports.Type || (exports.Type = {}));
  12. function applySubschema(it, appl, valid) {
  13. const subschema = getSubschema(it, appl);
  14. extendSubschemaData(subschema, it, appl);
  15. extendSubschemaMode(subschema, appl);
  16. const nextContext = { ...it, ...subschema, items: undefined, props: undefined };
  17. validate_1.subschemaCode(nextContext, valid);
  18. return nextContext;
  19. }
  20. exports.applySubschema = applySubschema;
  21. function getSubschema(it, { keyword, schemaProp, schema, strictSchema, schemaPath, errSchemaPath, topSchemaRef, }) {
  22. if (keyword !== undefined && schema !== undefined) {
  23. throw new Error('both "keyword" and "schema" passed, only one allowed');
  24. }
  25. if (keyword !== undefined) {
  26. const sch = it.schema[keyword];
  27. return schemaProp === undefined
  28. ? {
  29. schema: sch,
  30. schemaPath: codegen_1._ `${it.schemaPath}${codegen_1.getProperty(keyword)}`,
  31. errSchemaPath: `${it.errSchemaPath}/${keyword}`,
  32. }
  33. : {
  34. schema: sch[schemaProp],
  35. schemaPath: codegen_1._ `${it.schemaPath}${codegen_1.getProperty(keyword)}${codegen_1.getProperty(schemaProp)}`,
  36. errSchemaPath: `${it.errSchemaPath}/${keyword}/${util_1.escapeFragment(schemaProp)}`,
  37. };
  38. }
  39. if (schema !== undefined) {
  40. if (schemaPath === undefined || errSchemaPath === undefined || topSchemaRef === undefined) {
  41. throw new Error('"schemaPath", "errSchemaPath" and "topSchemaRef" are required with "schema"');
  42. }
  43. return {
  44. schema,
  45. strictSchema,
  46. schemaPath,
  47. topSchemaRef,
  48. errSchemaPath,
  49. };
  50. }
  51. throw new Error('either "keyword" or "schema" must be passed');
  52. }
  53. function extendSubschemaData(subschema, it, { dataProp, dataPropType: dpType, data, dataTypes, propertyName }) {
  54. if (data !== undefined && dataProp !== undefined) {
  55. throw new Error('both "data" and "dataProp" passed, only one allowed');
  56. }
  57. const { gen } = it;
  58. if (dataProp !== undefined) {
  59. const { errorPath, dataPathArr, opts } = it;
  60. const nextData = gen.let("data", codegen_1._ `${it.data}${codegen_1.getProperty(dataProp)}`, true);
  61. dataContextProps(nextData);
  62. subschema.errorPath = codegen_1.str `${errorPath}${getErrorPath(dataProp, dpType, opts.jsPropertySyntax)}`;
  63. subschema.parentDataProperty = codegen_1._ `${dataProp}`;
  64. subschema.dataPathArr = [...dataPathArr, subschema.parentDataProperty];
  65. }
  66. if (data !== undefined) {
  67. const nextData = data instanceof codegen_1.Name ? data : gen.let("data", data, true); // replaceable if used once?
  68. dataContextProps(nextData);
  69. if (propertyName !== undefined)
  70. subschema.propertyName = propertyName;
  71. // TODO something is possibly wrong here with not changing parentDataProperty and not appending dataPathArr
  72. }
  73. if (dataTypes)
  74. subschema.dataTypes = dataTypes;
  75. function dataContextProps(_nextData) {
  76. subschema.data = _nextData;
  77. subschema.dataLevel = it.dataLevel + 1;
  78. subschema.dataTypes = [];
  79. subschema.parentData = it.data;
  80. subschema.dataNames = [...it.dataNames, _nextData];
  81. }
  82. }
  83. function extendSubschemaMode(subschema, { compositeRule, createErrors, allErrors, strictSchema }) {
  84. if (compositeRule !== undefined)
  85. subschema.compositeRule = compositeRule;
  86. if (createErrors !== undefined)
  87. subschema.createErrors = createErrors;
  88. if (allErrors !== undefined)
  89. subschema.allErrors = allErrors;
  90. subschema.strictSchema = strictSchema; // not inherited
  91. }
  92. function getErrorPath(dataProp, dataPropType, jsPropertySyntax) {
  93. // let path
  94. if (dataProp instanceof codegen_1.Name) {
  95. const isNumber = dataPropType === Type.Num;
  96. return jsPropertySyntax
  97. ? isNumber
  98. ? codegen_1._ `"[" + ${dataProp} + "]"`
  99. : codegen_1._ `"['" + ${dataProp} + "']"`
  100. : isNumber
  101. ? codegen_1._ `"/" + ${dataProp}`
  102. : codegen_1._ `"/" + ${dataProp}.replace(/~/g, "~0").replace(/\\//g, "~1")`; // TODO maybe use global escapePointer
  103. }
  104. return jsPropertySyntax ? codegen_1.getProperty(dataProp).toString() : "/" + util_1.escapeJsonPointer(dataProp);
  105. }
  106. //# sourceMappingURL=subschema.js.map