ajv-validator / ajv-validator/ajv
removeAdditional not workig as expected
- Dominant language
- TypeScript
- Stars
- 14.8k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
**What version of Ajv are you using? Does the issue happen if you use the latest version?**
8.17.1
**Ajv options object**
```javascript
{
removeAdditional: true
}
```
**JSON Schema**
```json
export const schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/ClassProperties",
"definitions": {
"ClassProperties": {
"type": "object",
"properties": {
"additionalProperties": false,
"flags": {
"type": "array",
"items": {
"$ref": "#/definitions/Flags"
}
},
},
"propertyNames": {
"$ref": "#/definitions/StyleParams"
}
},
"StyleParams": {
"additionalProperties": false,
"type": "string",
"enum": [
"flags",
...Object.keys(StyleParameters) // includes "BACKGROUND_COLOR" but not "CHEESE"
]
},
"Flags": {
"type": "string",
"enum": [
"FLAG_ONE"
"FLAG_TWO"
]
}
}
} as const
```
**Sample data**
```json
{
flags: ["FLAG_ONE"],
BACKGROUND_COLOR: '#FFFFFF',
CHEESE: 'CHEDDAR'
}
```
**Your code**
```javascript
import Ajv from "ajv";
export class JsonValidationHelper {
enum StyleParameters {
BACKGROUND_COLOR = 'backgroundcolor'
// many more
}
private static JsonValidationHelper (typeProperties: object, schema: JSONSchema) {
const result = SchemaParser.ajv.validate(schema, typeProperties)
return typeProperties;
}
}
```
**Validation result, data AFTER validation, error messages**
```
validation result: false
data: {
flags: ["FLAG_ONE"],
BACKGROUND_COLOR: '#FFFFFF',
CHEESE: 'CHEDDAR'
}
```
**What results did you expect?**
Since removeAdditional is set to true in the options and additionalProperties are not allowed, all properties not defined in the schema should be removed and since there are no required properties set the validation result should always be true.
In short:
- The validation result should be true.
- property CHEESE schould have been removed from the data
**Are you going to resolve the issue?**
Contributor guide
Assessment
This issue has not been assessed yet.