frictionlessdata / tableschema-js

A JavaScript library for working with Table Schema.
http://frictionlessdata.io/
MIT License
82 stars 27 forks source link

Support enum for array items #152

Open JDziurlaj opened 5 years ago

JDziurlaj commented 5 years ago

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 commented 5 years ago

@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! :+1:

JDziurlaj commented 5 years ago

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

https://github.com/frictionlessdata/tableschema-js/blob/c2f71ecfe06d7d18243aecacece381067335e755/src/field.js#L166-L169

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 commented 5 years ago

@JDziurlaj Thanks! Now I see where is the problem - https://github.com/frictionlessdata/specs/issues/549. It's the specs related.