ajv-validator / ajv-validator/ajv

A `schemaType` of `integer` doesn't seem to work correctly with custom keyword

Abierto
#1,708 6 comentarios 0 reacciones 0 asignados Ver en GitHub
enhancement limitation
Lenguaje dominante
TypeScript
Estrellas
14.8k
Forks
1k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

8.6.2

**Ajv options object**

```javascript
{
strict: true,
allErrors: true,
logger: console,
loadSchema: () => {},
}
```

**JSON Schema**

```json
{
"myBadKeyword": 5,
}
```

**Your code**

```javascript
const Ajv = require('ajv');

async function testIt() {
const ajvOptions = {
strict: true,
allErrors: true,
logger: console,
loadSchema: () => {},
};

const ajv = new Ajv(ajvOptions);
ajv.addKeyword({
keyword: 'myBadKeyword',
schemaType: 'integer',
});

ajv.addKeyword({
keyword: 'myGoodKeyword',
schemaType: 'number',
});

const myBadSchema = {
myBadKeyword: 5,
};

const myGoodSchema = {
myGoodKeyword: 5,
};

try {
await ajv.compileAsync(myBadSchema);
console.log('bad schema compiled without exception');
} catch (e) {
console.log(e);
}

try {
await ajv.compileAsync(myGoodSchema);
console.log('good schema compiled without exception');
} catch (e) {
console.log(e);
}
}

testIt().then(console.log('done'));
```

Output:

```bash
✗ node test.js
done
Error: myBadKeyword value must be ["integer"]
at new KeywordCxt (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:298:23)
at keywordCode (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:449:17)
at /Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:222:17
at CodeGen.code (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:439:13)
at CodeGen.block (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:568:18)
at iterateKeywords (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:219:9)
at groupKeywords (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:208:13)
at /Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/validate/index.js:192:13
at CodeGen.code (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:439:13)
at CodeGen.block (/Users/cwelchmi/repos/metasys-rest-api/node_modules/ajv/dist/compile/codegen/index.js:568:18)
good schema compiled without exception
```

**Validation result, data AFTER validation, error messages**

Compiling the good schema with a custom keyword using `schemaType` of `number` works fine. Compiling the bad schema with a custom keyword using `schemaType` of `integer` throws an exception even though the data in the schema is an integer.

**What results did you expect?**

I expected that I could use `schemaType` of integer in this case.

**Are you going to resolve the issue?**

I just started looking into the issue. Not familiar with the code base, so I'm not sure.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.