remoteoss / json-schema-form

https://json-schema-form.vercel.app
MIT License
70 stars 7 forks source link

Custom Error Messages doesn't work in JSF Config #69

Open denatys opened 3 months ago

denatys commented 3 months ago

Given the following JSON Schema, I cannot override errorMessage using createHeadlessForm()

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "browsers": {
      "title": "Browsers (solo)",
      "description": "This solo select also includes a disabled option.",
      "type": "string",
      "oneOf": [
        {
          "const": "chr",
          "title": "Chrome"
        },
        {
          "const": "ff",
          "title": "Firefox"
        },
        {
          "const": "ie",
          "title": "Internet Explorer",
          "disabled": true
        }
      ],
      "x-jsf-presentation": {
        "inputType": "select"
      }
    },
    "has_pet": {
      "title": "Has Pet",
      "description": "Do you have a pet?",
      "oneOf": [
        {
          "title": "Yes",
          "const": "yes"
        },
        {
          "title": "No",
          "const": "no"
        }
      ],
      "x-jsf-presentation": {
        "inputType": "radio"
      },
      "type": "string"
    },
    "pet_name": {
      "title": "Pet's name",
      "description": "What's your pet's name?",
      "x-jsf-presentation": {
        "inputType": "text"
      },
      "type": "string",
      "errorMessage": "string"
    }
  },
  "required": ["has_pet"],
  "x-jsf-order": ["has_pet", "pet_name"],
  "allOf": [
    {
      "if": {
        "properties": {
          "has_pet": {
            "const": "yes"
          }
        },
        "required": ["has_pet"]
      },
      "then": {
        "required": ["pet_name"]
      },
      "else": {
        "properties": {
          "pet_name": false
        }
      }
    }
  ]
}

JSF Config:

    const { fields, handleValidation } = createHeadlessForm(Schema, {
      inputTypes: {
        errorMessage: {
          required: 'This cannot be empty.',
        },
      },
    });

This is very important to be able to i18n in the application

denatys commented 3 months ago
formErrors: pet_name: "Required field"
sandrina-p commented 3 months ago

Hi @denatys,

inputTypes only supports keys that match input types like text, number, etc. It does not support generic errorTypes. As the pet_name is a text, you need to pass its type like this:

const { fields, handleValidation } = createHeadlessForm(Schema, {
      inputTypes: {
        text: {
          required: 'This cannot be empty.',
        },
      },
    });
sandrina-p commented 3 months ago

We understand that this might not solve what you need (apply a given message to every error type of "required"), so this can be considered a missing feature. If you can, contributions are welcome ✌️