json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

validation against type when schema type keyword is an array

Open kuzinyu opened this issue 3 years ago • 4 comments

Hi,

I have a schema:

{"$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": {"a": {"type": "integer"}, "b": {"type": ["string", "null"]}}}

where the type of b is ["string", "null"] which according to json-schema should work like this:

The type keyword may either be a string or an array: If it’s a string, it is the name of one of the basic types above. If it is an array, it must be an array of strings, where each string is the name of one of the basic types, and each element is unique. In this case, the JSON snippet is valid if it matches any of the given types.

As I understand, both jsons should be valid: {"a": 1, "b": null} {"a": 1, "b": "s"}

When using json-schema-validator I get this error for {"a": 1, "b": null} - "$.b: null found, string expected"

Am I missing something?

Here's how I initialize my validator:

String schema = "..."

config = new SchemaValidatorsConfig() config.setFailFast(true)

schemaNode = objectMapper.readTree(schema) jsonSchema = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(schemaNode)).getSchema(schemaNode, config) jsonSchema.initializeValidators()

then i call jsonSchema.validate()

Does the library support arrays in type declaration? Any help please.

kuzinyu avatar Aug 16 '23 11:08 kuzinyu

I guess I have to set config.setFailFast(false) then it works as expected for union types.

kuzinyu avatar Aug 16 '23 12:08 kuzinyu

By default, failfast is false, and it is the default behaviour.

stevehu avatar Aug 25 '23 16:08 stevehu

I recently experienced same issue when using failFast set to true. It appears to be the same problem as reported in #246, with a mention that redesign is needed to address that issue. Do you have any updates or future plans to resolve this? Thank you for your hard work on this project.

Dukejung avatar Sep 06 '23 07:09 Dukejung

The faliFast is a flag for an API server to validate the incoming requests with the OpenAPI specification. It should be used in this context only as you want to return the error message as fast as possible. It should be set as false (default) in all other use cases.

stevehu avatar Sep 06 '23 13:09 stevehu