removeAdditional not workig as expected
Opened this issue · 1 comments
What version of Ajv are you using? Does the issue happen if you use the latest version?
8.17.1
Ajv options object
{
removeAdditional: true
}
JSON Schema
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
{
flags: ["FLAG_ONE"],
BACKGROUND_COLOR: '#FFFFFF',
CHEESE: 'CHEDDAR'
}
Your code
import Ajv from "ajv";
export class JsonValidationHelper {
enum StyleParameters {
BACKGROUND_COLOR = 'backgroundcolor'
// many more
}
private static JsonValidationHelper <T>(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?
As this does not seem to be a TS issue, can you provide a working example that doesn't use TypeScript and is as simple as you can make it. For example, rather than having ...Object.keys(StyleParameters)
you should just write the schema as a simple object literal.