Skip to content

Bug? Array in enum gets TypeError: left.charCodeAt is not a function #180

Open
@plocket

Description

This may be a leven package error, but I suspect it's just not being given the right argument types.

Expected behavior

A ajv regular error output

Actual behavior

A system error where the test fails to run:

 FAIL  ./test_schema.test.js
  ● Test suite failed to run

    TypeError: left.charCodeAt is not a function

      66 |   const options = get_options({ blocks });
      67 |   console.log(`------`, options.json);
    > 68 |   const unformatted_output = betterAjvErrors(schema, blocks, validate.errors, options);
         |                              ^
      69 |   const output_body = format_BAE_error_bodies({ output: unformatted_output });
      70 |   return { passed, output_body };
      71 | }

      at leven (node_modules/leven/index.js:27:33)
      at node_modules/better-ajv-errors/src/validation-errors/enum.js:63:17
          at Array.map (<anonymous>)
      at EnumValidationError.findBestMatch (node_modules/better-ajv-errors/src/validation-errors/enum.js:61:8)
      at EnumValidationError.getError (node_modules/better-ajv-errors/src/validation-errors/enum.js:30:28)
      at customErrorToStructure (node_modules/better-ajv-errors/src/index.js:11:49)
          at Array.map (<anonymous>)
      at src_default (node_modules/better-ajv-errors/src/index.js:22:25)
      at betterAjvErrors (test_schema.test.js:68:30)
      at Object.run_test (test_schema.test.js:52:35)

Notes

(see the suggestion lower down)

In leven (node_modules/leven/index.js:27:33), the problem seems to be that the left argument handed to leven() is an array, not a string. In my case I logged left: [ 'pdf' ] ; right: md. I think it's expecting a string for both arguments.

In better-ajv-errors, it was a bit hard to find things. I think the stack trace is mapped to a different file structure somehow. I didn't find a src folder. I dug around and found that findBestMatch() does miss the case where an enum value is an array.

I think it's this from

weight: leven(value, currentValue.toString()),

If you change value to value.toString(), that fixes the lib error. I'd make a PR, but in Atlassian's contributor agreement it asks for too much PI for my comfort.

Even with that fix, though, the error message is confusing as it doesn't note the array. For my schema (see below) a failing tests gets:

/attachment/0/valid formats must be equal to one of the allowed values: pdf, pdf. Did you mean pdf?

It would be clearer if it said something like:

/attachment/0/valid formats must be equal to one of the allowed values: pdf, [ 'pdf' ]. Did you mean pdf?

I think the relevant line in my schema is this:

            "valid formats": {
              "enum": ["pdf", ["pdf"]]
            }

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions