atlassian / atlassian/better-ajv-errors
Bug? Array in `enum` gets TypeError: left.charCodeAt is not a function
- Dominant language
- JavaScript
- Stars
- 245
- Forks
- 50
- PR merge metrics
- No merged PRs in 30d
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 ()
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 ()
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
https://github.com/atlassian/better-ajv-errors/blob/70356a517e452d774dbf5d480e1e8bf1d5d07388/src/validation-errors/enum.js#L63
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:
```json
"valid formats": {
"enum": ["pdf", ["pdf"]]
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with src/validation-errors/enum.js and findBestMatch(), using the failure and schema in test_schema.test.js as the reproduction. Run the existing test to verify enum arrays no longer cause the TypeError and that the resulting error message distinguishes the array value clearly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, json
- Domain
- backend-api-design, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100