Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

removeAdditional not workig as expected #2494

Open
ChrisKHil opened this issue Sep 9, 2024 · 1 comment
Open

removeAdditional not workig as expected #2494

ChrisKHil opened this issue Sep 9, 2024 · 1 comment

Comments

@ChrisKHil
Copy link

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?
@jasoniangreen
Copy link
Collaborator

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Development

No branches or pull requests

2 participants