-
Notifications
You must be signed in to change notification settings - Fork 61
Unexpected InvalidJsonPointerException when no errors limit set and required property is missing #12
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
Comments
This is happening right now, but when using
This might solve your problem, but it can also introduce unexpected behavior. According to json-pointer specs, when resolving a json pointer and the value is nonexistent I have only two options: either throw an error/exception and stop, or use a fallback value and continue. However, you can look at this issue from two different angles Perspective AThe problem is that the json pointer references a nonexisting value, so this can be solved in two ways: 1. Remove the referenceThis is the quickest solution and requires no change in the opis/json-schema lib. However, you must create a new filter and move it one level up {
"type": "object",
"properties": {
"password": {"type": "string"},
"repeatPassword": {"type": "string"}
},
"required": ["password", "repeatPassword"],
"$filters": {
"$func": "propsAreEqual",
"$vars": {"left": "password", "right": "repeatPassword"}
}
} And the filter in php // propsAreEqual
function ($value, array $args): bool {
if (!property_exists($value, $args['left'])) {
return false;
}
if (!property_exists($value, $args['right'])) {
return false;
}
return $value->{$args['left']} === $value->{$args['right']};
} 2. Fallback to a default valueThis requires a small change in the opis/json-schema lib in order to provide a fallback value. Example of schema (if the changes are made) {
"type": "object",
"properties": {
"password": {"type": "string"},
"passwordRepeat": {
"type": "string",
"$filters": {
"$func": "sameAs",
"$vars": {
"value": {
"$ref": "1/password",
"default": null
}
}
}
}
},
"required": ["password", "passwordRepeat"]
} Now, when the Perspective BThe problem is that Well, this might not sound so good if you are trying to validate a form and display the appropriate error messages for every form field. In the next major release the |
Thank you for your detailed response, I evaluated and tested your suggestion (perspective A) before submitting this issue, but it didn't feel elegant, and I wanted to make you aware of it, because the behaviour I expect seems more consistent to me, and maybe for other people as well.
Yes I want that, I intent do validate a client request (browser) and I want to return in the same response as much errors as possible .
This option isn't good for me, error validation belong to the backend in my case, mainly to keep consistency and to prevent logic duplication.
Well I see it a bit different. If the user receive too many errors depends of the interface/layout/design/... and can be very subjective. We don't need to discuss about that. In my case I want to give feedback immediately and try to avoid the user gets new errors on each (request) try, what can be quite annoying.
Ok, I understand why you plan to remove |
Well, to me looks like it can lead to undefined behaviour because it is not explicit (like A.2.) but it relies on other keywords (such as "required"). On a complex schema it is hard to figure what is really going on.
I've already proposed a change (A.2. Fallback to a default value) to fix this issue, so there is no reason to say that. Plus, a PR is always welcome if you have better idea/solution. On the other hand, there can be an A.3. solution: since passwordRepeat depends on password you can use dependencies keyword. {
"type": "object",
"properties": {
"password": {"type": "string"}
},
"required": ["password", "passwordRepeat"],
"dependencies": {
"password": {
"properties": {
"passwordRepeat": {
"type": "string",
"$filters": {
"$func": "sameAs",
"$vars": {
"value": {
"$ref": "1/password"
}
}
}
}
}
}
}
} |
We do have a hierarchical error system, but at a deeper analysis it seems that |
I would say A.1 has an advantage over A.2 in the sense that A.1 allows comparing values of any type, while Can it be so that a developer cannot ensure existence of a property, but still refers to it with a JSON pointer? Maybe just fail the |
In {
"if": {
"$filters": {
"$func": "data-exists",
"$vars": {
"ref": "/path/to/data"
}
}
},
"then": {
"$comment": "/path/to/data exists"
},
"else": {
"$comment": "cannot resolve /path/to/data"
}
} |
I wouldn't expect an exception Opis...\InvalidJsonPointerException, when a missing path/property is required. For me, required properties should have a higher "priority" and references should be checked only after requirements of a higher "order" are meet.
If I take your example and change the $data variable (remove "password") and set no errors limit (-1) I get such exception:
I would expect a validation error (ValidationError) instead. In my opinion, an exception would be appropriate only when "password" is not required (and missing of course).
The text was updated successfully, but these errors were encountered: