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

Support enum for array items #152

Open
JDziurlaj opened this issue Sep 26, 2018 · 3 comments
Open

Support enum for array items #152

JDziurlaj opened this issue Sep 26, 2018 · 3 comments
Labels

Comments

@JDziurlaj
Copy link
Contributor

I've run into a situation where the definition of a field as type: "array" and an enum constraint results in the fieldName becoming listed as undefined

const TF = {
  fields: [{
    "name": "ApplicationRequestStatusType",
    "type": "array",
    "title": "Application Request Status",
    "description": "description goes here.",
    "constraints": {
      "required": true,
      "enum": [
        "duplicate",
        "invalid",
        "valid",
        "other"
      ]
    }
  }],
}

// Tests
describe('Schema', () => {

  it('specify correct fieldName', async () => {
    {
      const schema = await Schema.load(TF)
     //FAIL
     assert.equal(schema.fieldNames[0], "ApplicationRequestStatusType")
    }
    delete TF.fields[0].constraints
    {
      const schema = await Schema.load(TF)
      //PASS
      assert.equal(schema.fieldNames[0], "ApplicationRequestStatusType")
    }

  })
@roll
Copy link
Member

roll commented Oct 1, 2018

@JDziurlaj
Don't you mind taking a look at https://github.com/frictionlessdata/tableschema-js/blob/master/src/schema.js if you can? It's probably a simple bug there. Unfortunately, we don't have time for fixing it the upcoming weeks.

And thank you for reporting! 👍

@JDziurlaj
Copy link
Contributor Author

Hi @roll,

From what I can tell, the error is caused by the Field class (src/field.js) trying to cast the enum constraint (lines 166-169). This causes each enumeration literal to be cast by the Field's data type (array), and throwing an exception when the literal is parsed as JSON, i.e. JSON.parse("enum-value").

tableschema-js/src/field.js

Lines 166 to 169 in c2f71ec

// Cast enum constraint
if (['enum'].includes(name)) {
castConstraint = constraint.map(cast)
}

I'm not sure what the purpose of the cast is here (perhaps to normalize input?), but commenting lines 166-169 out seems to fix this issue, while not causing any regressions on the test suite.

@roll
Copy link
Member

roll commented Oct 8, 2018

@JDziurlaj
Thanks! Now I see where is the problem - frictionlessdata/datapackage#549. It's the specs related.

@roll roll changed the title field typed array and enum constraint causes undefined Support enum for array items May 20, 2019
@roll roll added the feature label May 20, 2019
@roll roll closed this as completed Apr 6, 2020
@roll roll reopened this Apr 6, 2020
@roll roll added review and removed review labels Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants