Skip to content

✨ Proposal: at least one property is required #1602

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

Closed
DarkLite1 opened this issue May 8, 2025 · 7 comments
Closed

✨ Proposal: at least one property is required #1602

DarkLite1 opened this issue May 8, 2025 · 7 comments
Labels
proposal Initial discussion of a new idea. A project will be created once a proposal document is created.

Comments

@DarkLite1
Copy link

Describe the inspiration for your proposal

It would be great if it was possible to define that either field y or x is required.

Describe the proposal

If you want to send an email you can either have a to field or bcc field or both, but none is not acceptable.

"To": {
    "type": "array",
    "title": "To",
    "description": "An array of recipient email addresses.",
    "items": {
    "type": "string",
    "format": "email"
    }
},
"Bcc": {
    "type": "array",
    "title": "Bcc",
    "description": "An array of BCC email addresses.",
    "items": {
    "type": "string",
    "format": "email"
    }
},

Describe alternatives you've considered

No response

Additional context

No response

@DarkLite1 DarkLite1 added the proposal Initial discussion of a new idea. A project will be created once a proposal document is created. label May 8, 2025
@DarkLite1 DarkLite1 changed the title ✨ Proposal: Either or ✨ Proposal: at least one property is required May 8, 2025
@gregsdennis
Copy link
Member

Hey there. Thanks for the idea.

This is quite easy to do with the current keywords:

{
   "anyOf": [
     { "required": [ "to" ] },
     { "required": [ "bcc" ] }
   ]
}

This requires that at least one of the properties to be present.

@DarkLite1
Copy link
Author

Thank you, and I'm sorry for not reading the docs properly. Your suggestion works with a small tweak as below.

schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:hm:schemas:move-file-sftp-config",
  "type": "object",
  "title": "Move File Over SFTP Configuration",
  "description": "Configuration schema for moving files over SFTP.",
  "properties": {
    "Settings": {
      "type": "object",
      "title": "Settings",
      "description": "Various script settings.",
      "properties": {
        "SendMail": {
          "type": "object",
          "title": "Send Mail Settings",
          "description": "Settings for sending email notifications.",
          "properties": {
            "To": {
              "type": "array",
              "title": "To",
              "description": "An array of recipient email addresses.",
              "items": { "type": "string", "format": "email" },
              "uniqueItems": true
            },
            "Bcc": {
              "type": "array",
              "title": "Bcc",
              "description": "An array of BCC email addresses.",
              "items": { "type": "string", "format": "email" },
              "uniqueItems": true
            }
          },
          "required": [],
          "anyOf": [
            { "required": ["To"], "properties": { "To": { "minItems": 1 } } },
            { "required": ["Bcc"], "properties": { "Bcc": { "minItems": 1 } } }
          ],
          "errorMessage": "Either 'To' or 'Bcc' must contain at least one email address."
        }
      },
      "required": ["SendMail"]
    }
  },
  "required": ["Settings"]
}

test.json

{
  "Settings": {
    "SendMail": {
      "To": ["[email protected]"],
      "Bcc": []
    }
  }
}

@DarkLite1
Copy link
Author

Hmmm, errorMessage does not seem to work. It throws:

The JSON is not valid with the schema: Value should have at least 1 items at '/Settings/SendMail/To'

Instead of the string

Either 'To' or 'Bcc' must contain at least one email address.

Found a similar issue on StackOverflow.

@gregsdennis
Copy link
Member

errorMessage isn't a JSON Schema keyword. It's not going to do anything. It might be something that the implementation you're using has decided to support. It'd be best to ask them.

@DarkLite1
Copy link
Author

Ah, got it. Thanks for clarifying. So there's no way to have a custom error message in case an anyOf fails?

@gregsdennis
Copy link
Member

Custom error messages would be an implementation thing. JSON Schema doesn't mandate any kind of messaging.

@DarkLite1
Copy link
Author

Would be a great new feature I think. But thx anyway, this was really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Initial discussion of a new idea. A project will be created once a proposal document is created.
Projects
None yet
Development

No branches or pull requests

2 participants