ajv-validator / ajv-validator/ajv

Schema validation does not always fail when using custom meta schema

Ouverte
#1,605 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
limitation thinking
Langage dominant
TypeScript
Étoiles
14.8k
Forks
1k
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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

**Ajv options object**

```javascript
{}
```

**JSON Schema**
```json
{
"$schema": "http://Custom",
"type": "object",
"properties": {
"foo": { "oneOf": [
{"type": "string"},
{"type": "number"}
]}
}
}
```

**Sample data**
```json
{"foo": "abc"}
```

**Your code**

```javascript
const meta = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://Custom",
"title": "Custom",
"definitions": {
"simpleTypes": {
"enum": ["array", "boolean", "integer", "null", "number", "object", "string"]
},
"stringArray": {
"type": "array",
"items": {"type": "string"},
"uniqueItems": true,
"default": []
}
},
"type": ["object", "boolean"],
"properties": {
"$schema": {
"type": "string",
"format": "uri"
},
"default": true,
"additionalProperties": {"$ref": "#"},
"properties": {
"type": "object",
"additionalProperties": {"$ref": "#"},
"default": {}
},
"type": {
"anyOf": [
{"$ref": "#/definitions/simpleTypes"},
{
"type": "array",
"items": {"$ref": "#/definitions/simpleTypes"},
"minItems": 1,
"uniqueItems": true
}
]
},
"format": {"type": "string"},
},
"additionalProperties": false, // added
"default": true
};

ajv.addMetaSchema(meta);

const compileAndValidate = () => {
try
{
var validate = ajv.compile(schema);

console.log(validate(data));
console.log(validate.errors);
}
catch (e) {
console.error(e);
}
}
console.log("first run");
compileAndValidate();
console.log("second run");
compileAndValidate();
```
http://runkit.com/remyblok/ajv-issue-custom-schema-validation-issue

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

```
"first run"
Error: schema is invalid: data/properties/foo must NOT have additional properties
"second run"
true
null
```

**What results did you expect?**
I'm trying to make a custom schema based on the default draft-7 schema, but without complex features, like oneOf, anyOf etc. So the schema *should* fail validation. I expect the validation of the schema to always fail if it is invalid according to the meta schema.

**Are you going to resolve the issue?**
I've traced the error back to premature caching of the schema in _addSchema(). The schema is cached here:
https://github.com/ajv-validator/ajv/blob/df964e43cbd10cf16c1ee07a71c0c6a2698f10d2/lib/core.ts#L684
But the schema is validated after that at: https://github.com/ajv-validator/ajv/blob/df964e43cbd10cf16c1ee07a71c0c6a2698f10d2/lib/core.ts#L690
The second time it just gets the cached schema without validation.
I'm not sure if I can just move the cache to after the validate?

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.