Skip to content

Add support for optional enums #15

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

Closed
JoakimLofgren opened this issue Aug 10, 2012 · 2 comments
Closed

Add support for optional enums #15

JoakimLofgren opened this issue Aug 10, 2012 · 2 comments

Comments

@JoakimLofgren
Copy link
Contributor

Specification

From the latest JSON schema draft:

5.19. enum
This provides an enumeration of all possible values that are valid
for the instance property. This MUST be an array, and each item in
the array represents a possible value for the instance value. If
this attribute is defined, the instance value MUST be one of the
values in the array in order for the schema to be valid.
Comparison
of enum values uses the same algorithm as defined in "uniqueItems"
(Section 5.15).

If I interpret the text in bold correctly then the JSON would be valid:

  • if the attribute is omitted
  • and if required is false

When running this in another JSON schema library this seems to hold true.

Testing

Running JSV with the following JSON structure:

{
    "status": "running"
}

Against a schema like this one:

{
    "type": "object",
    "properties": {
        "status": {
            "type": "string",
            "enum": [
                "started", "stopped"
            ],
            "required": false
        }
    }
}

Produces the following output

[ { uri: 'urn:uuid:b3801fd2-b377-4beb-b7ea-7e22a1e01a02#/status',
    schemaUri: 'urn:uuid:dc175d99-1145-479c-b455-19fcf6cd30ad#/properties/status',
    attribute: 'enum',
    message: 'Instance is not one of the possible values',
    details: [ 'started', 'stopped' ] } ]

If I however run the same schema against this JSON:

{
    "ignore": "me"
}

It validates just fine.

Running this in json-schema produces the following output:

[status] does not have a value in the enumeration started, stopped
@fidian
Copy link
Contributor

fidian commented Dec 20, 2012

Would you elaborate as to why required would have bearing on this? The spec doesn't mentioned required and only says that if enum is defined then the value in the object must match a value in the enum array.

@JoakimLofgren
Copy link
Contributor Author

I was just being overly clear.
"required": false is the same as not having it.

It behaved as if required was set to true.

This issue was actually merged in my pull request.
#16

So maybe this should be closed?

erayd added a commit to erayd/json-schema that referenced this issue Mar 14, 2017
erayd added a commit to erayd/json-schema that referenced this issue Mar 17, 2017
erayd added a commit to erayd/json-schema that referenced this issue Mar 17, 2017
erayd added a commit to erayd/json-schema that referenced this issue Mar 21, 2017
erayd added a commit to erayd/json-schema that referenced this issue Mar 21, 2017
bighappyface pushed a commit that referenced this issue Mar 21, 2017
* Improve performance - don't loop over everything if already valid

* Don't coerce already-valid types (fixes #379)

* Add remaining coercion cases & rewrite tests

 * Add all remaining coercion cases from ajv matrix
 * Rewrite the coercion tests to tidy things up a bit

* Add CHECK_MODE_EARLY_COERCE

If set, falls back to the old behavior of coercing to the first
compatible type, regardless of whether another already-valid type might
be available.

* Add multiple-type test that requires coercion

* \JSON_PRETTY_PRINT doesn't exist in PHP-5.3, so work around this

* Various PR cleanup stuff

 * Fix whitespace
 * Turn $early into $extraFlags
 * Change "string" to "ABC" in string test
 * Update README.md description of CHECK_MODE_EARLY_COERCE

* Move loop after complex tests definition

* Move test #39 to grid #15
erayd added a commit to erayd/json-schema that referenced this issue Mar 22, 2017
* Improve performance - don't loop over everything if already valid

* Don't coerce already-valid types (fixes jsonrainbow#379)

* Add remaining coercion cases & rewrite tests

 * Add all remaining coercion cases from ajv matrix
 * Rewrite the coercion tests to tidy things up a bit

* Add CHECK_MODE_EARLY_COERCE

If set, falls back to the old behavior of coercing to the first
compatible type, regardless of whether another already-valid type might
be available.

* Add multiple-type test that requires coercion

* \JSON_PRETTY_PRINT doesn't exist in PHP-5.3, so work around this

* Various PR cleanup stuff

 * Fix whitespace
 * Turn $early into $extraFlags
 * Change "string" to "ABC" in string test
 * Update README.md description of CHECK_MODE_EARLY_COERCE

* Move loop after complex tests definition

* Move test jsonrainbow#39 to grid jsonrainbow#15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants