Skip to content

Customer Error Message: "errorMessage" does not work with if / then / else blocks #1175

@AHOCHWE

Description

@AHOCHWE

🐛 Bug Report / Feature Request

The errorMessage keyword does not appear to support if / then / else blocks.


What I'm trying to do

I want to define custom error messages for conditional schemas using if / then logic, like this:

{
  "if": {
    "properties": {
      "type": { "const": "business" }
    }
  },
  "then": {
    "required": ["businessId"]
  },
  "errorMessage": {
    "if": "Type condition failed",
    "then": "Missing businessId when type is 'business'",
    "else": "Missing required fields for other types"
  }
}

I also tried:

"errorMessage": "Custom message for conditional failure"

and

"then": {
  "required": ["someField"],
  "errorMessage": {
    "required": "someField must be provided"
  }
}

Actual Behavior

  • The schema validates correctly
  • But custom messages in errorMessage for if, then, or else are ignored
  • Instead, it shows the default errors

Expected Behavior

I expected that:

"errorMessage": {
  "then": "Missing businessId when type is 'business'"
}

…would override the default message when the then clause fails (i.e., businessId is missing).

Minimal Reproducible Schema

{
  "$schema": "https://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "type": { "type": "string" },
    "businessId": { "type": "string" }
  },
  "if": {
    "properties": {
      "type": { "const": "business" }
    },
    "required": ["type"]
  },
  "then": {
    "required": ["businessId"]
  },
  "errorMessage": {
    "then": "Missing 'businessId' when 'type' is 'business'"
  }
}

Expected Result

Error message returned should be:

Missing 'businessId' when 'type' is 'business'

Questions

  • Is errorMessage for if/then/else unsupported? Or am I doing something wrong?
  • If not supported, would you consider this a useful feature to add?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions