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

Validation for optional objects #54

Closed
nitsnwits opened this issue Oct 14, 2014 · 3 comments
Closed

Validation for optional objects #54

nitsnwits opened this issue Oct 14, 2014 · 3 comments

Comments

@nitsnwits
Copy link

First of all, thank you for a great library. I've found it really useful and its 'options' features is the one which I could not find in other libraries.

I was working with different schema validations for a project. Let's say, my whole JSON body is empty and the schema defines all the fields are optional.

  1. Is there a way to define that even though all the fields are optional, at least one (any of it) should be present in the body and the body should not be empty?
  2. If my JSON body has two fields A and B and the schema defines A and B as optional. Is there a way to define that if A is present, then, B is required?

It may not be a bug, but I could find the documentation supporting such features.

Thank you for your time.

Neeraj

@zaggino
Copy link
Owner

zaggino commented Oct 14, 2014

By the specification, you should use anyOf for the 1) but to make it easier for you can use custom format defined like this:

ZSchema.registerFormat("hasAtLeastOneProperty", function (obj) {
    return Object.keys(obj).length > 0;
});

@zaggino
Copy link
Owner

zaggino commented Oct 14, 2014

For 2) you should use dependencies, take a look at http://json-schema.org/latest/json-schema-validation.html#anchor70

Here's a test suite that might give you more ideas on how to use it:

{
        "description": "dependencies",
        "schema": {
            "dependencies": {"bar": ["foo"]}
        },
        "tests": [
            {
                "description": "neither",
                "data": {},
                "valid": true
            },
            {
                "description": "nondependant",
                "data": {"foo": 1},
                "valid": true
            },
            {
                "description": "with dependency",
                "data": {"foo": 1, "bar": 2},
                "valid": true
            },
            {
                "description": "missing dependency",
                "data": {"bar": 2},
                "valid": false
            },
            {
                "description": "ignores non-objects",
                "data": "foo",
                "valid": true
            }
        ]
    }

https://github.com/json-schema/JSON-Schema-Test-Suite/blob/develop/tests/draft4/dependencies.json

@nitsnwits
Copy link
Author

Thank you for the feedback, @zaggino That really helped solve me the problem. Closing this issue.

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

No branches or pull requests

2 participants