Enum constraint with arrays in table schema
See original GitHub issueI’m using an array field with an enum constraint that consists of an array of strings:
...
{
"name": "ApplicationRequestStatusType",
"type": "array",
"title": "Application Request Status",
"description": "Specifies the current status of the application. …",
"constraints": {
"required": true,
"enum": [
"duplicate",
"invalid",
"missing-ssn",
"missing-state-id-number",
"pending",
"valid"
]
}
}
...
I’m trying to save a datapackage–using Package–but running into validation errors. The error that’s being thrown is Field "ApplicationRequestStatusType" can't cast value "duplicate" for type "array" with format “default"
.
From my Gitter conversation with @roll:
@jungshadow It’s interesting the specs says that enum constraint is applicable to an array field type. But as an implementator I’m confused here. Now it should work but not the way you expect. Because it uses a general approach (as for other types) every enum item should be an array. And for users I think this behaviour doesn’t really make sense if it’s not clarified in the specs. @rufuspollock should the specs specify a special approach for treating an enum constraint for arrays/objects? Or probably it should be just a different constraint like constraints.itemEnum? It’s also related to the typed arrays discussion
I believe I’m implementing this correctly and it looks like both types of enum constraints (e.g. array of strings and array of arrays) are supported based on the table-schema.json file:
"constraints": {
"title": "Constraints",
"description": "The following constraints apply for `array` fields.",
"type": "object",
"properties": {
...
},
"unique": {
...
},
"enum": {
"oneOf": [
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string"
}
},
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "array"
}
}
]
},
...
}
},
Is my assessment that my previous snippet is technically correct, accurate? My use case is to constrain an array (prefer a set in this case) to a list of potential values (the strings in the example above). Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:18 (8 by maintainers)
Top GitHub Comments
This is a perfect application of the ‘itemType’ property I suggested here: https://github.com/frictionlessdata/specs/issues/409
So - you’d have an array, and you’ll be able to state the inner item type (in this case, a string with an enum constraint).
I appreciate the quick fix, @roll! This isn’t a knock on the fix and maybe I’m being overly pedantic, but this functionally seems more like an array than a string. Interested in @pwalsh’s and @rufuspollock’s thoughts on this, too.