better-ajv-errors icon indicating copy to clipboard operation
better-ajv-errors copied to clipboard

Error when data value is undefined

Open ZeRego opened this issue 3 years ago • 0 comments

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.

ZeRego avatar Sep 06 '22 10:09 ZeRego