2 Object.defineProperty(exports, "__esModule", { value: true });
3 const code_1 = require("../code");
4 const codegen_1 = require("../../compile/codegen");
5 const names_1 = require("../../compile/names");
6 const util_1 = require("../../compile/util");
8 message: "must NOT have additional properties",
9 params: ({ params }) => (0, codegen_1._) `{additionalProperty: ${params.additionalProperty}}`,
12 keyword: "additionalProperties",
14 schemaType: ["boolean", "object"],
19 const { gen, schema, parentSchema, data, errsCount, it } = cxt;
20 /* istanbul ignore if */
22 throw new Error("ajv implementation error");
23 const { allErrors, opts } = it;
25 if (opts.removeAdditional !== "all" && (0, util_1.alwaysValidSchema)(it, schema))
27 const props = (0, code_1.allSchemaProperties)(parentSchema.properties);
28 const patProps = (0, code_1.allSchemaProperties)(parentSchema.patternProperties);
29 checkAdditionalProperties();
30 cxt.ok((0, codegen_1._) `${errsCount} === ${names_1.default.errors}`);
31 function checkAdditionalProperties() {
32 gen.forIn("key", data, (key) => {
33 if (!props.length && !patProps.length)
34 additionalPropertyCode(key);
36 gen.if(isAdditional(key), () => additionalPropertyCode(key));
39 function isAdditional(key) {
41 if (props.length > 8) {
42 // TODO maybe an option instead of hard-coded 8?
43 const propsSchema = (0, util_1.schemaRefOrVal)(it, parentSchema.properties, "properties");
44 definedProp = (0, code_1.isOwnProperty)(gen, propsSchema, key);
46 else if (props.length) {
47 definedProp = (0, codegen_1.or)(...props.map((p) => (0, codegen_1._) `${key} === ${p}`));
50 definedProp = codegen_1.nil;
52 if (patProps.length) {
53 definedProp = (0, codegen_1.or)(definedProp, ...patProps.map((p) => (0, codegen_1._) `${(0, code_1.usePattern)(cxt, p)}.test(${key})`));
55 return (0, codegen_1.not)(definedProp);
57 function deleteAdditional(key) {
58 gen.code((0, codegen_1._) `delete ${data}[${key}]`);
60 function additionalPropertyCode(key) {
61 if (opts.removeAdditional === "all" || (opts.removeAdditional && schema === false)) {
62 deleteAdditional(key);
65 if (schema === false) {
66 cxt.setParams({ additionalProperty: key });
72 if (typeof schema == "object" && !(0, util_1.alwaysValidSchema)(it, schema)) {
73 const valid = gen.name("valid");
74 if (opts.removeAdditional === "failing") {
75 applyAdditionalSchema(key, valid, false);
76 gen.if((0, codegen_1.not)(valid), () => {
78 deleteAdditional(key);
82 applyAdditionalSchema(key, valid);
84 gen.if((0, codegen_1.not)(valid), () => gen.break());
88 function applyAdditionalSchema(key, valid, errors) {
90 keyword: "additionalProperties",
92 dataPropType: util_1.Type.Str,
94 if (errors === false) {
95 Object.assign(subschema, {
101 cxt.subschema(subschema, valid);
105 exports.default = def;
106 //# sourceMappingURL=additionalProperties.js.map