guillotinaweb / ngx-schema-form

HTML form generation based on JSON Schema
MIT License
485 stars 174 forks source link

Visible If not working if value is [0] #431

Open AleksaDursun opened 2 years ago

AleksaDursun commented 2 years ago

When my selectField value is 0 the schema does not render the fields that have visibleIf: {selectField: [0]}. This is the code in my schema. { type: 'array', title: 'Columns', items: { type: 'object', properties: { color: { type: 'string', description: 'Color', widget: 'color-picker', } }, }, visibleIf: { selectField: [0] }, }

daniele-pecora commented 2 years ago

A working example on Stackblitz (click) for your schema:

{
  "type": "object",
  "properties": {
    "selectField": {
      "type": "string",
      "widget": "select",
      "oneOf": [
        {
          "enum": ["0"],
          "description": "Zero"
        },
        {
          "enum": ["1"],
          "description": "One"
        }
      ]
    },
    "columns": {
      "type": "array",
      "title": "Columns",
      "items": {
        "type": "object",
        "properties": {
          "color": {
            "type": "string",
            "description": "Color",
            "widget": "color-picker"
          }
        }
      },
      "visibleIf": {
        "selectField": [0]
      }
    }
  }
}

@AleksaDursun would you take a look on it?

AleksaDursun commented 2 years ago

That is the workaround I found as well, but the issue still persists. If the value is equal to 0 (number zero) it does not work.

KBroichhausen commented 2 years ago

Got the same problem. As far as I can tell it's related to this line: https://github.com/guillotinaweb/ngx-schema-form/blob/f80c7a5b0646828dc3ab16973c0f3ecbf938467a/projects/schema-form/src/lib/model/formproperty.ts#L252

The truth value of variable value is false if it is equal to the number 0 and double negation still makes it false. This is probably to check if there is no null or undefined in the variable value but this also makes the values NaN, number 0, boolean false and empty string '' to not pass this check.

My workaround is to use "$EXP$ target.value == 0"