ajv-validator / ajv-validator/ajv
"TypeError: Reduce of empty array with no initial value" when compiling parser for discriminator without properties
- Lingua principale
- TypeScript
- Stelle
- 14.8k
- Fork
- 1k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
**What version of Ajv are you using? Does the issue happen if you use the latest version?**
8.20
**Ajv options object**
empty / default
**JSON Schema**
```json
{
"discriminator": "t",
"mapping": {
"a": {
"properties": {}
},
"b": {
"properties": {
"b": { "type": "int32" }
}
},
}
}
```
**Your code**
```javascript
import Ajv from "ajv/dist/jtd.js"
new Ajv().compileParser(schema);
```
**Validation result, data AFTER validation, error messages**
```
node_modules/ajv/dist/compile/codegen/index.js:682
return args.reduce(andCode);
^
TypeError: Reduce of empty array with no initial value
at Array.reduce ()
at and (node_modules/ajv/dist/compile/codegen/index.js:682:17)
at parseSchemaProperties (node_modules/ajv/dist/compile/jtd/parse.js:208:44)
at parseDiscriminator (node_modules/ajv/dist/compile/jtd/parse.js:170:9)
at parseNullable (node_modules/ajv/dist/compile/jtd/parse.js:105:16)
at parseCode (node_modules/ajv/dist/compile/jtd/parse.js:97:9)
at node_modules/ajv/dist/compile/jtd/parse.js:78:9
at CodeGen.code (node_modules/ajv/dist/compile/codegen/index.js:439:13)
at CodeGen.func (node_modules/ajv/dist/compile/codegen/index.js:587:18)
at parserFunction (node_modules/ajv/dist/compile/jtd/parse.js:71:9)
Node.js v25.9.0
```
**What results did you expect?**
That the schema compiles a parser without error, compiling a validator works.
**Are you going to resolve the issue?**
I'm unfamiliar with how the code generator for the parser works - I could adjust the code in https://github.com/ajv-validator/ajv/blob/master/lib/compile/jtd/parse.ts#L234 to check if the properties object is not empty
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.