Skip to content

Add the "contains" keyword for array instances. #113

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

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions jsonschema-validation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,17 @@
</t>
</section>

<section title="contains">
<t>
The value of this keyword MUST be an object. This object MUST be
a valid JSON Schema.
</t>
<t>
An array instance is valid against "contains" if at least one of
its elements is valid against the given schema.
</t>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or array is empty

Copy link
Member

@awwright awwright Oct 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I want an array to contain an item, I would think that means empty array should always be invalid.

I don't see any reason to have a special behavior for an edge case.

Is there a case I would want to treat an empty array instance as valid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "or array is empty" is easily handled by:

{
    "type": "array",
    "items": {...},
    "oneOf": [
        {"contains": {...}},
        {"maxItems": 0}
    ]
}

Copy link
Member

@epoberezkin epoberezkin Oct 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It follows from contains being equivalent to:

{
    "not": {
        "items": {
            "not": {... whatever ...}
        }
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or it doesn't? :)

Copy link
Member

@epoberezkin epoberezkin Oct 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, ok, should have at least one item :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, an empty array fails that schema, because an empty array passes the schema "items": {"not": {...}}, so the outer not causes it to fail overall.

</section>

<section title="maxProperties">
<t>
The value of this keyword MUST be an integer. This integer MUST be
Expand Down
1 change: 1 addition & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"type": "boolean",
"default": false
},
"contains": { "$ref": "#" },
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
Expand Down