Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'match' of undefined #94

Closed
nam-hle opened this issue Aug 29, 2021 · 3 comments
Closed

TypeError: Cannot read property 'match' of undefined #94

nam-hle opened this issue Aug 29, 2021 · 3 comments

Comments

@nam-hle
Copy link

nam-hle commented Aug 29, 2021

Hi all, I'm suffering an issue while trying to show validate a JSON file. The below are my model, schema, and script files.

Here is my TS interface file (model.ts)

export interface Model {
	readonly defaultRowAction?: DefaultRowAction;
}

export interface DefaultRowAction {
	readonly custom: boolean;
	readonly event: string;
}

The schema is generated by ts-json-schema-generator package (schema.json)

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/Model",
  "definitions": {
    "Model": {
      "type": "object",
      "properties": {
        "defaultRowAction": {
          "$ref": "#/definitions/DefaultRowAction"
        }
      },
      "additionalProperties": false
    },
    "DefaultRowAction": {
      "type": "object",
      "properties": {
        "custom": {
          "type": "boolean"
        },
        "event": {
          "type": "string"
        }
      },
      "required": [
        "custom",
        "event"
      ],
      "additionalProperties": false
    }
  }
}

My testing JSON file (model.json):

{
  "defaultRowAction" : {
    "custom" : false
  }
}

The scripting (index.js):

const Ajv = require("ajv");
const betterAjvErrors = require("better-ajv-errors");

const model = require("./model.json");
const schema = require("./schema.json");

const isValid = new Ajv().compile(schema);
if (!isValid(model)) {
	console.log(betterAjvErrors(schema, model, isValid.errors));
}

The output errors:

node index.js
PATH/node_modules/better-ajv-errors/lib/modern/helpers.js:26
    const paths = dataPath === '' ? [''] : dataPath.match(JSON_POINTERS_REGEX);
                                                    ^

TypeError: Cannot read property 'match' of undefined
    at PATH/node_modules/better-ajv-errors/lib/modern/helpers.js:26:53
    at Array.forEach (<anonymous>)
    at makeTree (PATH/node_modules/better-ajv-errors/lib/modern/helpers.js:21:13)
    at _default (PATH/node_modules/better-ajv-errors/lib/modern/helpers.js:116:16)
    at _default (PATH/node_modules/better-ajv-errors/lib/modern/index.js:27:45)
    at Object.<anonymous> (PATH/scripts/index.js:9:14)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)

The environment and package versions:

MacOS darwin x86_64
Node: 14.17.0
Npm: 6.14.13
ajv: 8.6.2
better-ajv-errors: 0.7.0
ts-json-schema-generator: 0.95.0

@henrikrudstrom
Copy link

I got the same error:
this is my schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/product.schema.json",
  "title": "ordino engine-config",
  "description": "flette config for ordino",
  "type": "object",
  "definitions": {
    "mixers": {
      "fixed-positions": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "const": "fixed-positions" },
          "positions": {
            "type": "object",
            "additionalProperties": {
              "type": "array",
              "items": { "type": "number" }
            }
          },
          "pin": {
            "type": "array",
            "items": { "type": "string" }
          }
        },
        "additionalProperties": false,
        "required": ["name"]
      },
      "weighed-sort": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "const": "weighted-sort" }
        },
        "additionalProperties": false,
        "required": ["name"]
      }
    },
    "postprocessors": {
      "sort-by-score": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "const": "sort-by-score" },
          "additionalProperties": false,
          "required": ["name"]
        }
      },
      "assign-score": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "const": "assign-score" },
          "fixed": {
            "type": "object",
            "properties": { "value": { "type": "number" } },
            "additionalProperties": false,
            "required": ["value"]
          },
          "linear": {
            "type": "object",
            "properties": {
              "min": { "type": "number" },
              "max": { "type": "number" }
            },
            "additionalProperties": false,
            "required": ["min", "max"]
          },
          "geometric": {
            "type": "object",
            "properties": {
              "max": { "type": "number" },
              "ratio": { "type": "number" }
            },
            "additionalProperties": false,
            "required": ["ratio", "max"]
          }
        },
        "oneOf": [
          { "required": ["name", "fixed"] },
          { "required": ["name", "linear"] },
          { "required": ["name", "geometric"] }
        ]
      }
    },
    "source": {
      "type": "object",
      "properties": {
        "type": { "type": "string", "const": "source" },
        "name": { "type": "string" },
        "source": { "type": "string" },
        "usePreferences": { "type": "boolean" },
        "weight": { "type": "number" },
        "postProcess": {
          "oneOf": [
            { "$ref": "#/definitions/postprocessors/sort-by-score" },
            { "$ref": "#/definitions/postprocessors/assign-score" }
          ]
        },
        "teasers": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "frontpageID": { "type": "string" },
              "siteKey": { "type": "string" },
              "source": { "type": "string" },
              "score": { "type": "number" }
            },
            "additionalProperties": false,
            "required": ["id"]
          }
        }
      },
      "additionalProperties": false,
      "oneOf": [
        { "required": ["type", "name", "source"] },
        { "required": ["type", "name", "teasers"] }
      ]
    },
    "composition": {
      "type": "object",
      "properties": {
        "type": { "type": "string", "const": "composition" },
        "name": { "type": "string" },
        "mixer": {
          "oneOf": [
            { "$ref": "#/definitions/mixers/fixed-positions" },
            { "$ref": "#/definitions/mixers/weighed-sort" }
          ]
        },
        "postProcess": {
          "oneOf": [
            { "$ref": "#/definitions/postprocessors/sort-by-score" },
            { "$ref": "#/definitions/postprocessors/assign-score" }
          ]
        },
        "weight": { "type": "number" },
        "sources": {
          "type": "array",
          "items": {
            "oneOf": [
              { "$ref": "#/definitions/source" },
              { "$ref": "#/definitions/composition" }
            ]
          }
        }
      },
      "additionalProperties": false,
      "required": ["name", "mixer", "sources"]
    }
  },
  "oneOf": [
    { "$ref": "#/definitions/source" },
    { "$ref": "#/definitions/composition" }
  ]
}

data is

{
  "type": "composition",
  "name": "root",
  "mixer": { "name": "weighted-sort" },
  "sources": [
    {
      "type": "source",
      "name": "frontpage",
      "postProcess": {
        "name": "assig-score",
        "linear": {
          "min": 0.1,
          "max": 1.0
        }
      },
      "source": "frontpage",
      "usePreferences": true
    }
  ]
}

@janstuemmel
Copy link

probably and issue with version 7/8 of ajv. They removed the option jsonPointer: boolean and better-ajv relies on that i think. Try downgrading ajv to version 6

@torifat
Copy link
Collaborator

torifat commented Nov 3, 2021

This should be fixed by #92

@torifat torifat closed this as completed Nov 3, 2021
orgads pushed a commit to orgads/better-ajv-errors that referenced this issue Dec 3, 2023
chore(deps): update dependency jest to v29.1.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants