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

Default JSON Schema type validation does not take to account whether a field is required #3901

Open
Chalolennox opened this issue Apr 24, 2024 · 1 comment
Labels

Comments

@Chalolennox
Copy link

Chalolennox commented Apr 24, 2024

Description
I am playing around with the json schema examples(https://formly.dev/docs/examples/advanced/json-schema) and realised the schema depedency example with bootstrap does not set validation on the field credit_card correctly when I clear the input after typing in a value. This field is optional. I suspect the validator for number type is active.

Minimal Reproduction

{
    "title": "Schema dependencies",
    "description": "These samples are best viewed without live validation.",
    "type": "object",
    "properties": {
        "simple": {
            "title": "Simple",
            "type": "object",
            "properties": {
                "credit_card": {
                    "type": "number",
                    "title": "Credit card"
                }
            },
            "dependencies": {
                "credit_card": {
                    "properties": {
                        "billing_address": {
                            "type": "string",
                            "title": "Billing address"
                        }
                    },
                    "required": [
                        "billing_address"
                    ]
                }
            }
        }
    }
}

I have tried the react version of the library on https://rjsf-team.github.io/react-jsonschema-form/ and it works as intended in that when I clear the credit_card field, the form is valid again. As soon as I type a value and clears it, formly shows a validation error. Here is the active validator:

({
        value
      }) => {
        if (value === void 0) {
          return true;
        }
        if (value === null && types.indexOf("null") !== -1) {
          return true;
        }
        switch (types[0]) {
          case "null": {
            return typeof value === null;
          }
          case "string": {
            return typeof value === "string";
          }
          case "integer": {
            return isInteger(value);
          }
          case "number": {
            return typeof value === "number";
          }
          case "object": {
            return isObject(value);
          }
          case "array": {
            return Array.isArray(value);
          }
        }
        return true;
      }

is there a better way to deal with nullable values which are not required when validating?

@Chalolennox
Copy link
Author

A work around would be to extend formlyJsonschema.toFieldConfig

@Chalolennox Chalolennox changed the title JSON Schema example with dependecies does not reset validation Default JSON Schema type validation does not take to account whether a field is required Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant