atlassian / better-ajv-errors

JSON Schema validation for Human 👨‍🎤
https://atlassian.github.io/better-ajv-errors/
Other
232 stars 44 forks source link

Error when data value is undefined #154

Open ZeRego opened 2 years ago

ZeRego commented 2 years ago

Our code:

betterAjvErrors(
    lightdashDbtYamlSchema,
    schemaFile,
    validate.errors || [],
    { indent: 2 },
);

Values:

schemaFile is undefined

validate.errors is

[
  {
    instancePath: '',
    schemaPath: '#/type',
    keyword: 'type',
    params: { type: 'object' },
    message: 'must be object'
  }
]

Error:

e TypeError: Cannot read properties of undefined (reading 'charAt')
    at next (/Users/joserego/Documents/lightdash/node_modules/@humanwhocodes/momoa/api.js:235:22)
    at tokenize (/Users/joserego/Documents/lightdash/node_modules/@humanwhocodes/momoa/api.js:491:13)
    at parse (/Users/joserego/Documents/lightdash/node_modules/@humanwhocodes/momoa/api.js:726:20)
    at Object.src_default (/Users/joserego/Documents/lightdash/packages/cli/node_modules/better-ajv-errors/lib/cjs/index.js:31:42)
    at /Users/joserego/Documents/lightdash/packages/cli/dist/dbt/schema.js:55:49
    at Generator.next (<anonymous>)
    at fulfilled (/Users/joserego/Documents/lightdash/packages/cli/dist/dbt/schema.js:24:58)

Workaround

By making sure the data defaults to an empty string or an empty object or null.

betterAjvErrors(
    lightdashDbtYamlSchema,
    schemaFile || '',
    validate.errors || [],
    { indent: 2 },
);

And with this get the proper error:

TYPE must be object

> 1 | ""
    | ^^ 👈🏽  type must be object

Solution:

better-ajv-errors could make sure data can't be undefined. Or support undefined and have an appropriate message.