Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

AdditionalPropertiesKeyword error incorrectly thrown #148

Open
brentscheffler opened this issue Jan 20, 2025 · 4 comments
Open

AdditionalPropertiesKeyword error incorrectly thrown #148

brentscheffler opened this issue Jan 20, 2025 · 4 comments

Comments

@brentscheffler
Copy link

If additionalProperties is set to false and a property value falls outside of the enum, an additional schema error of Additional object properties are not allowed is also returned.

Expected behavior

Only the The data should match one item from enum error should be returned.

Schema

{
	"type":"object",
	"properties": {
		"name": {
			"type": "string",
			"enum": ["apples", "oranges", "bananas"]
		},
		"published_at": {
			"type": "string",
			"format": "date-time"
		}
	},
	"required": ["name", "published_at"],
	"additionalProperties":false
}

Data

{
	"name": "kiwis",
	"published_at": "2025-01-20T05:06:49+00:00"
}

Error

#message: "The properties must match schema: {properties}"
      #subErrors: array:1 [
        0 => Opis\JsonSchema\Errors\ValidationError^ {#195
          #keyword: "enum"
          #schema: Opis\JsonSchema\Schemas\ObjectSchema^ {#215}
          #data: Opis\JsonSchema\Info\DataInfo^ {#216
            #value: "kiwis"
            #type: "string"
            #root: {#188}
            #path: array:1 [
              0 => "name"
            ]
            #parent: null
            #fullPath: null
          }
          #args: []
          #message: "The data should match one item from enum"
          #subErrors: []
        }
      ]
    }
    1 => Opis\JsonSchema\Errors\ValidationError^ {#219
      #keyword: "additionalProperties"
      #schema: Opis\JsonSchema\Schemas\ObjectSchema^ {#212}
      #data: Opis\JsonSchema\Info\DataInfo^ {#220
        #value: {#188}
        #type: "object"
        #root: {#188}
        #path: []
        #parent: null
        #fullPath: null
      }
      #args: array:1 [
        "properties" => array:2 [
          0 => "name"
          1 => "published_at"
        ]
      ]
      #message: "Additional object properties are not allowed: {properties}"
      #subErrors: []
    }
@sorinsarca
Copy link
Member

It is because you probably have $validator->setStopAtFirstError(false);
This is a good example of why validation should stop when an error occured, some keywords should be reached only if data is valid so far.

I will try to make a fix, but I think this won't be the last time.

@brentscheffler
Copy link
Author

So enabling $validator->setStopAtFirstError(true); results in potential false positives?

@sorinsarca
Copy link
Member

So enabling $validator->setStopAtFirstError(true); results in potential false positives?

No, it should not. This is a bug and I'll add a fix.

@brentscheffler
Copy link
Author

Understood - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants