question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enum constraint with arrays in table schema

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
akarivcommented, Dec 25, 2017

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).

1reaction
jungshadowcommented, Dec 18, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enums as arrays in PostgreSQL - Peter Oliha | oliha.dev
Internally, PostgreSQL creates a constraint check for the table column that we created an enum type for. Enum column with multiple values. So ......
Read more >
Check Enum in tableschema.r: Table Schema 'Frictionless Data'
Check if the value is exactly match a constraint. Usage. constraints.checkEnum(constraint, value). Arguments. constraint. numeric list,matrix or ...
Read more >
How to read a column definition of type ENUM & convert it to ...
Now, in Java, I'm using JPA & want to read the column definition to convert the col3 enum values to array \ list....
Read more >
How to map a PostgreSQL Enum ARRAY to a JPA entity ...
Learn how to map the PostgreSQL enum column types to JPA entity properties using Hibernate and the hibernate-types open-source project.
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found