Skip to content

Instantly share code, notes, and snippets.

@mefellows
Last active July 29, 2023 00:26
Show Gist options
  • Select an option

  • Save mefellows/283f130d5db6fad33b805b54b3416ec6 to your computer and use it in GitHub Desktop.

Select an option

Save mefellows/283f130d5db6fad33b805b54b3416ec6 to your computer and use it in GitHub Desktop.

Revisions

  1. mefellows revised this gist Jul 29, 2023. 1 changed file with 0 additions and 16 deletions.
    16 changes: 0 additions & 16 deletions package-lock.json
    Original file line number Diff line number Diff line change
    @@ -27,22 +27,6 @@
    "url": "https://github.com/sponsors/epoberezkin"
    }
    },
    "node_modules/ajv-formats": {
    "version": "2.1.1",
    "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz",
    "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==",
    "dependencies": {
    "ajv": "^8.0.0"
    },
    "peerDependencies": {
    "ajv": "^8.0.0"
    },
    "peerDependenciesMeta": {
    "ajv": {
    "optional": true
    }
    }
    },
    "node_modules/fast-deep-equal": {
    "version": "3.1.3",
    "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
  2. mefellows revised this gist Jul 29, 2023. 2 changed files with 2 additions and 4 deletions.
    3 changes: 1 addition & 2 deletions package-lock.json
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,7 @@
    "version": "1.0.0",
    "license": "ISC",
    "dependencies": {
    "ajv": "^8.12.0",
    "ajv-formats": "^2.1.1"
    "ajv": "^8.12.0"
    }
    },
    "node_modules/ajv": {
    3 changes: 1 addition & 2 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,6 @@
    "author": "",
    "license": "ISC",
    "dependencies": {
    "ajv": "^8.12.0",
    "ajv-formats": "^2.1.1"
    "ajv": "^8.12.0"
    }
    }
  3. mefellows revised this gist Jul 29, 2023. 3 changed files with 4 additions and 43 deletions.
    15 changes: 1 addition & 14 deletions allOf.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,8 @@
    const Ajv = require("ajv/dist/2019");
    const addFormats = require("ajv-formats");
    const assert = require("assert")

    const validateJson = (schema, json) => {
    const ajv = new Ajv({
    allErrors: true,
    discriminator: true,
    logger: false,
    strictSchema: false,
    });

    addFormats(ajv);

    ajv.addKeyword({
    keyword: "collectionFormat",
    type: "array",
    });
    const ajv = new Ajv();

    ajv.validate(schema, json);

    17 changes: 2 additions & 15 deletions nested.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,8 @@
    const Ajv = require("ajv/dist/2019");
    const addFormats = require("ajv-formats");
    const assert = require("assert")

    const validateJson = (schema, json) => {
    const ajv = new Ajv({
    allErrors: true,
    discriminator: true,
    logger: false,
    strictSchema: false,
    });

    addFormats(ajv);

    ajv.addKeyword({
    keyword: "collectionFormat",
    type: "array",
    });
    const ajv = new Ajv()

    ajv.validate(schema, json);

    @@ -35,7 +22,7 @@ const schema = {
    },
    },
    {
    // unevaluatedProperties: false, // <- need to comment this out for the tests to pass
    unevaluatedProperties: false, // <- need to comment this out for the tests to pass
    allOf: [
    {
    type: "object",
    15 changes: 1 addition & 14 deletions nestedComplex.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,8 @@
    const Ajv = require("ajv/dist/2019");
    const addFormats = require("ajv-formats");
    const assert = require("assert");

    const validateJson = (schema, json) => {
    const ajv = new Ajv({
    allErrors: true,
    discriminator: true,
    logger: false,
    strictSchema: false,
    });

    addFormats(ajv);

    ajv.addKeyword({
    keyword: "collectionFormat",
    type: "array",
    });
    const ajv = new Ajv()

    ajv.validate(schema, json);

  4. mefellows revised this gist Jul 28, 2023. 2 changed files with 107 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion nested.js
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ const schema = {
    },
    },
    {
    // unevaluatedProperties: false, // <- need to comment this out for the tests to pass
    allOf: [
    {
    type: "object",
    @@ -44,6 +45,14 @@ const schema = {
    },
    },
    },
    {
    type: "object",
    properties: {
    baz: {
    type: "string",
    },
    },
    },
    ],
    },
    ],
    @@ -53,11 +62,12 @@ const schema = {
    const validPayload = {
    foo: "bar",
    bar: "baz",
    baz: "qux"
    };

    const invalidPayload = {
    ...validPayload,
    baz: "qux"
    qux: "quux"
    };

    var errors = validateJson(schema, validPayload);
    96 changes: 96 additions & 0 deletions nestedComplex.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    const Ajv = require("ajv/dist/2019");
    const addFormats = require("ajv-formats");
    const assert = require("assert");

    const validateJson = (schema, json) => {
    const ajv = new Ajv({
    allErrors: true,
    discriminator: true,
    logger: false,
    strictSchema: false,
    });

    addFormats(ajv);

    ajv.addKeyword({
    keyword: "collectionFormat",
    type: "array",
    });

    ajv.validate(schema, json);

    return ajv.errors || [];
    };

    const schema = {
    type: "object",
    unevaluatedProperties: false,
    allOf: [
    {
    type: "object",
    properties: {
    foo: {
    type: "string",
    },
    },
    },
    {
    type: "object",
    properties: {
    bar: {
    unevaluatedProperties: false,
    allOf: [
    {
    type: "object",
    properties: {
    baz: {
    type: "string",
    },
    },
    },
    {
    type: "object",
    properties: {
    bat: {
    type: "string",
    },
    },
    },
    ],
    },
    },
    },
    ],
    };

    const validPayload = {
    foo: "bar",
    bar: {
    baz: "bat",
    bat: "quz",
    },
    };

    const invalidPayload = {
    foo: "bar",
    bar: {
    baz: "bat",
    bat: "quz",
    qux: "quux",
    },
    qux: "quux",
    };

    var errors = validateJson(schema, validPayload);
    console.log("valid payload errors:");
    console.dir(errors, { depth: 5 });
    assert(errors.length === 0, "valid payload should not have errors");

    console.log("");

    var errors = validateJson(schema, invalidPayload);
    console.log("invalid payload errors:");
    console.dir(errors, { depth: 5 });
    assert(errors.length > 0, "invalid payload should have errors");

    console.log("✅ PASSED!");
  5. mefellows revised this gist Jul 27, 2023. 2 changed files with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,8 @@
    Demonstrates nested `allOf` issue
    Demonstrates nested `allOf` with AJV:

    ```
    node allOf.js
    node nested.js
    ```

    A non-zero exit code indicates a problem.
    File renamed without changes.
  6. mefellows revised this gist Jul 27, 2023. 6 changed files with 245 additions and 1 deletion.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    node_modules
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    Demonstrates nested `allOf` issue in AVJ.
    Demonstrates nested `allOf` issue
    70 changes: 70 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    const Ajv = require("ajv/dist/2019");
    const addFormats = require("ajv-formats");
    const assert = require("assert")

    const validateJson = (schema, json) => {
    const ajv = new Ajv({
    allErrors: true,
    discriminator: true,
    logger: false,
    strictSchema: false,
    });

    addFormats(ajv);

    ajv.addKeyword({
    keyword: "collectionFormat",
    type: "array",
    });

    ajv.validate(schema, json);

    return ajv.errors || [];
    };

    const schema = {
    type: "object",
    unevaluatedProperties: false,
    allOf: [
    {
    type: "object",
    properties: {
    foo: {
    type: "string",
    },
    },
    },
    {
    type: "object",
    properties: {
    bar: {
    type: "string",
    },
    },
    },
    ],
    };

    const validPayload = {
    foo: "bar",
    bar: "baz",
    };

    const invalidPayload = {
    ...validPayload,
    baz: "qux"
    };

    var errors = validateJson(schema, validPayload);
    console.log("valid payload errors:");
    console.dir(errors, { depth: 5 });
    assert(errors.length === 0, "valid payload should not have errors");

    console.log("");

    var errors = validateJson(schema, invalidPayload);
    console.log("invalid payload errors:");
    console.dir(errors, { depth: 5 });
    assert(errors.length === 1, "invalid payload should have errors");

    console.log("✅ PASSED!");
    75 changes: 75 additions & 0 deletions nested.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    const Ajv = require("ajv/dist/2019");
    const addFormats = require("ajv-formats");
    const assert = require("assert")

    const validateJson = (schema, json) => {
    const ajv = new Ajv({
    allErrors: true,
    discriminator: true,
    logger: false,
    strictSchema: false,
    });

    addFormats(ajv);

    ajv.addKeyword({
    keyword: "collectionFormat",
    type: "array",
    });

    ajv.validate(schema, json);

    return ajv.errors || [];
    };

    const schema = {
    type: "object",
    unevaluatedProperties: false,
    allOf: [
    {
    type: "object",
    properties: {
    foo: {
    type: "string",
    },
    },
    },
    {
    allOf: [
    {
    type: "object",
    properties: {
    bar: {
    type: "string",
    },
    },
    },
    ],
    },
    ],
    };


    const validPayload = {
    foo: "bar",
    bar: "baz",
    };

    const invalidPayload = {
    ...validPayload,
    baz: "qux"
    };

    var errors = validateJson(schema, validPayload);
    console.log("valid payload errors:");
    console.dir(errors, { depth: 5 });
    assert(errors.length === 0, "valid payload should not have errors");

    console.log("");

    var errors = validateJson(schema, invalidPayload);
    console.log("invalid payload errors:");
    console.dir(errors, { depth: 5 });
    assert(errors.length === 1, "invalid payload should have errors");

    console.log("✅ PASSED!");
    82 changes: 82 additions & 0 deletions package-lock.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    {
    "name": "allof-repro",
    "version": "1.0.0",
    "lockfileVersion": 3,
    "requires": true,
    "packages": {
    "": {
    "name": "allof-repro",
    "version": "1.0.0",
    "license": "ISC",
    "dependencies": {
    "ajv": "^8.12.0",
    "ajv-formats": "^2.1.1"
    }
    },
    "node_modules/ajv": {
    "version": "8.12.0",
    "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.12.0.tgz",
    "integrity": "sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==",
    "dependencies": {
    "fast-deep-equal": "^3.1.1",
    "json-schema-traverse": "^1.0.0",
    "require-from-string": "^2.0.2",
    "uri-js": "^4.2.2"
    },
    "funding": {
    "type": "github",
    "url": "https://github.com/sponsors/epoberezkin"
    }
    },
    "node_modules/ajv-formats": {
    "version": "2.1.1",
    "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz",
    "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==",
    "dependencies": {
    "ajv": "^8.0.0"
    },
    "peerDependencies": {
    "ajv": "^8.0.0"
    },
    "peerDependenciesMeta": {
    "ajv": {
    "optional": true
    }
    }
    },
    "node_modules/fast-deep-equal": {
    "version": "3.1.3",
    "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
    "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
    },
    "node_modules/json-schema-traverse": {
    "version": "1.0.0",
    "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
    "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="
    },
    "node_modules/punycode": {
    "version": "2.3.0",
    "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz",
    "integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==",
    "engines": {
    "node": ">=6"
    }
    },
    "node_modules/require-from-string": {
    "version": "2.0.2",
    "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
    "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==",
    "engines": {
    "node": ">=0.10.0"
    }
    },
    "node_modules/uri-js": {
    "version": "4.4.1",
    "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
    "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==",
    "dependencies": {
    "punycode": "^2.1.0"
    }
    }
    }
    }
    16 changes: 16 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    {
    "name": "allof-repro",
    "version": "1.0.0",
    "description": "Demonstrates nested `allOf` issue in AVJ.",
    "main": "index.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
    "ajv": "^8.12.0",
    "ajv-formats": "^2.1.1"
    }
    }
  7. mefellows created this gist Jul 27, 2023.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Demonstrates nested `allOf` issue in AVJ.