Skip to content
This repository has been archived by the owner on Mar 31, 2020. It is now read-only.

failed to validate array with different types of complex objects #31

Closed
sstrigler opened this issue Nov 18, 2014 · 6 comments
Closed

failed to validate array with different types of complex objects #31

sstrigler opened this issue Nov 18, 2014 · 6 comments
Assignees
Labels

Comments

@sstrigler
Copy link
Contributor

As requested this is a follow up on #30.

When using this schema as vnd.1and1.mams.user-agenda-modify-v1+json:

{
    "title" : "AgendaModify",
    "type" : "object",
    "properties" : {
        "add" : {
            "type" : "object",
            "required" : true,
            "properties" : {
                "contacts" : {
                    "type" : "array",
                    "required" : true,
                    "items" : {
                        "type" : [
                            {
                                "type" : "object",
                                "properties" : {
                                    "phone" : {
                                        "type": "string",
                                        "pattern" : "^\+\d{10,15}$",
                                        "required" : true
                                    }
                                },
                                "additionalProperties" : false
                            },
                            {
                                "type" : "object",
                                "properties" : {
                                    "email" : {
                                        "type" : "string",
                                        "pattern" : "^\S+@\S+\.\S+$",
                                        "required" : true
                                    }
                                },
                                "additionalProperties" : false
                            }
                        ]
                    },
                    "additionalItems" : false
                }
            },
            "additionalProperties" : false
        }
    },
    "additionalProperties" : false
}

calling jesse:validate on

<<"vnd.1and1.mams.user-agenda-modify-v1+json">>,
<<"{\n  \"add\": {\n           \"contacts\": [\n                         { \"phone\": \"+46737341553\" },\n                         { \"email\": \"alice@web.de\" }\n                       ]\n         }\n}">>

results in the following (unexpected) error

  [
   {data_invalid,
    {struct,[{<<"type">>,[{struct,[{<<"type">>,<<"object">>},{<<"properties">>,{struct,[{<<"phone">>,{struct,[{<<"type">>,<<"string">>\
},{<<"pattern">>,<<"^\\+\\d{10,15}$">>},{<<"required">>,true}]}}]}},{<<"additionalProperties">>,false}]},{struct,[{<<"type">>,<<"objec\
t">>},{<<"properties">>,{struct,[{<<"email">>,{struct,[{<<"type">>,<<"string">>},{<<"pattern">>,<<"^\\S+@\\S+\\.\\S+$">>},{<<"required\
">>,true}]}}]}},{<<"additionalProperties">>,false}]}]}]},
    wrong_type,
    {struct,[{<<"email">>,<<"alice@web.de">>}]},[<<"add">>,<<"contacts">>,1]},
   {data_invalid,
    {struct,[{<<"type">>,[{struct,[{<<"type">>,<<"object">>},{<<"properties">>,{struct,[{<<"phone">>,{struct,[{<<"type">>,<<"string">>\
},{<<"pattern">>,<<"^\\+\\d{10,15}$">>},{<<"required">>,true}]}}]}},{<<"additionalProperties">>,false}]},{struct,[{<<"type">>,<<"objec\
t">>},{<<"properties">>,{struct,[{<<"email">>,{struct,[{<<"type">>,<<"string">>},{<<"pattern">>,<<"^\\S+@\\S+\\.\\S+$">>},{<<"required\
">>,true}]}}]}},{<<"additionalProperties">>,false}]}]}]},
    wrong_type,
    {struct,[{<<"phone">>,<<"+46737341553">>}]},[<<"add">>,<<"contacts">>,0]}
  ].
@sstrigler sstrigler added the bug label Nov 18, 2014
@sstrigler
Copy link
Contributor Author

Or maybe more readable:

(msg_accountmanagement@localhost)2> jesse:validate(<<"vnd.1and1.mams.user-agenda-modify-v1+json">>, <<"{\"add\": { \"contacts\": [{\"phone\": \"+23945827342\"}]}}">>, [{parser_fun, fun mochijson2:decode/1},{allowed_errors, 5}]).
{error,[{data_invalid,{struct,[{<<"type">>,
                                [{struct,[{<<"type">>,<<"object">>},
                                          {<<"properties">>,
                                           {struct,[{<<"phone">>,
                                                     {struct,[{<<"type">>,<<"string">>},
                                                              {<<"pattern">>,<<"^\\+\\"...>>},
                                                              {<<"requ"...>>,true}]}}]}},
                                          {<<"additionalProperties">>,false}]},
                                 {struct,[{<<"type">>,<<"object">>},
                                          {<<"properties">>,
                                           {struct,[{<<"email">>,
                                                     {struct,[{<<"type">>,<<"stri"...>>},
                                                              {<<"patt"...>>,<<...>>},
                                                              {<<...>>,...}]}}]}},
                                          {<<"additionalProperties">>,false}]}]}]},
                      wrong_type,
                      {struct,[{<<"phone">>,<<"+23945827342">>}]},
                      [<<"add">>,<<"contacts">>,0]}]}
(msg_accountmanagement@localhost)3>

@sstrigler
Copy link
Contributor Author

Further investigation has shown that jesse_schema_validator:validate_with_state/3 within check_union_type throws this error:

{data_invalid,
           {struct,
               [{<<"type">>,
                 [{struct,
                      [{<<"type">>,<<"object">>},
                       {<<"properties">>,
                        {struct,
                            [{<<"phone">>,
                              {struct,
                                  [{<<"type">>,<<"string">>},
                                   {<<"pattern">>,<<"^\\+\\d{10,15}$">>},
                                   {<<"required">>,true}]}}]}},
                       {<<"additionalProperties">>,false}]},
                  {struct,
                      [{<<"type">>,<<"object">>},
                       {<<"properties">>,
                        {struct,
                            [{<<"email">>,
                              {struct,
                                  [{<<"type">>,<<"string">>},
                                   {<<"pattern">>,<<"^\\S+@\\S+\\.\\S+$">>},
                                   {<<"required">>,true}]}}]}},
                       {<<"additionalProperties">>,false}]}]}]},
           no_extra_properties_allowed,
           {struct,[{<<"phone">>,<<"+23945827342">>}]},
           [<<"add">>,<<"contacts">>,0,<<"phone">>]}

when checking for type:

{struct,[{<<"type">>,<<"object">>},
               {<<"properties">>,
                {struct,[{<<"phone">>,
                          {struct,[{<<"type">>,<<"string">>},
                                   {<<"pattern">>,<<"^\\+\\d{10,15}$">>},
                                   {<<"required">>,true}]}}]}},
               {<<"additionalProperties">>,false}]}

I'm puzzled about this 'no_extra_properties_allowed'...

@sstrigler sstrigler self-assigned this Nov 19, 2014
@Cy6erBr4in
Copy link
Contributor

well, works for me, I get {ok, ...}

btw, when I copied the schema from the first comment from you, jiffy could parse that complaining on the regexp strings, it seems it had some invisible unicode crap, so then I copied regexps from another comment, and it worked well.

just to double check, what erlang version do you use? I'll test it on the same (currently I have R16B03 from default Ubuntu distribution)

@sstrigler
Copy link
Contributor Author

Hey, please check out my new pull request.

I'm using Erlang/OTP 17.3 ... but having a hard time believing it could be the version used.

@sstrigler
Copy link
Contributor Author

And yeah, sorry, I don't know what's wrong with the regexp patterns here. If you go on "edit" for the first comment here it all looks nice but open safe it makes this weird thing. When saving it as a gist it works fine though: https://gist.github.com/sstrigler/aed786948d4f6f3d5ee1

Cy6erBr4in added a commit that referenced this issue Nov 20, 2014
Fix validation for array with set of types (refs #31)
@Cy6erBr4in
Copy link
Contributor

fixed in 1.1.4

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

No branches or pull requests

2 participants