jonasschmidt / ex_json_schema

An Elixir JSON Schema validator
MIT License
366 stars 98 forks source link

Validation issues #89

Closed 0xAX closed 9 months ago

0xAX commented 9 months ago

Hello,

I am trying to validate basic JSON example and have troubles. Can't understand, do I do something wrong with the ex_json_schema library or something wrong with my JSON schema.

The JSON schema is:

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "properties": {
        "auth": {
            "description": "",
            "type": "object",
            "$ref": "#/$defs/proxy_server"     
        }
    },    
    "$defs": {
        "proxy_server": {
            "type": "object",
            "oneOf": [
                {
                    "type": "object",
                    "additionalProperties": false
                },
                {
                    "patternProperties": {
                        ".*": { "type": "object", "$ref": "#/$defs/server" }
                    }
                }
            ]
        },
        "server": {
            "type": "object",
            "properties": {
                "listen": {
                    "type": "string",
                    "format": "ip-address"                    
                }
            },
            "required": ["listen"]
        }
    }
}

I want to have an object that will be or {"auth": {}} or {"auth": {"server": {"listen": ....}}}. But even if we'll omit nested examples, the:

ExJsonSchema.Validator.validate(schema, %{"auth": 1})

returns :ok while it clearly should fail because auth has to be an object.

The schema after resolving is:

schema #{'__struct__' => 'Elixir.ExJsonSchema.Schema.Root',
         custom_format_validator => nil,definitions => #{},location => root,
         refs => #{},
         schema => 
             #{<<"$defs">> =>
                   #{<<"proxy_server">> =>
                         #{<<"oneOf">> =>
                               [#{<<"additionalProperties">> => false,
                                  <<"properties">> => #{},
                                  <<"type">> => <<"object">>}],
                           <<"type">> => <<"object">>},
                     <<"server">> =>
                         #{<<"properties">> =>
                               #{<<"listen">> =>
                                     #{<<"format">> => <<"ip-address">>,
                                       <<"type">> => <<"string">>}},
                           <<"required">> => [<<"listen">>],
                           <<"type">> => <<"object">>}},
               <<"$id">> => <<"http://example.com/example.json">>,
               <<"$schema">> => <<"http://json-schema.org/draft-07/schema">>,
               <<"properties">> =>
                   #{<<"auth">> =>
                         #{<<"$ref">> => [root,<<"$defs">>,<<"proxy_server">>],
                           <<"description">> => <<>>,
                           <<"type">> => <<"object">>}},
               <<"type">> => <<"object">>},
         version => 7}

Could you please give me a hint what could be wrong?

0xAX commented 9 months ago

resolved, the keys should be strings