1
votes

I have a property in my JSON schema like this:

"properties": {
  "theme": {
    "type": "boolean",
    "title": "Theme",
    "enum": ["Light", "Dark"]
  }
}

This property is stored in my database table as a boolean, with 1 for light, 0 for dark. The problem is that when I fill out the form generated by the schema it errors, telling me that the value of root.theme must be one of [true, false] and won't proceed to my backend handler.

There is a solution I found that I'm currently utilising, which is changing the "type" to string, and handling it on the backend side, by converting the string value I receive into the boolean corresponding to it.

I am wondering whether JSON schema natively supports custom naming for true & false in a boolean property.

1
Hi Ivan. I think you might have a number of missconceptions here about how things work. JSON Schema uses JSON. In JSON, true and false are their own values. I don't know what you mean by "custom naming". Can you expand on this please?Relequestual
Hi. I am aware that true and false are their own primitives, what I meant is was it possible to use a boolean type JSON schema property (I use the schema to build a form) that would provide the front end of the application with string values for the end user to select, while providing me with a boolean value on form submit. Naturally, I could achieve it via additional javascript manipulation of my template/handle it via the backend where the form data gets sent to, but I was wondering whether such additional code is necessary if I want the user to see Light|Dark and the system get t|fIvan T.
JSON Schema is for validating JSON. It wasn't designed to be used for generating forms, but some have implemented means to make a schema into a form. Simply, if you're using JSON Schema to do anything that isn't just validation, you'll have to either roll your own, or log an issue with the tool you're using. It's unlikely that data manipulation falls into the same tool as form generation though. Generally you'll handle the sort of thing you describe in your ORM's inflation and deflation layer to avoid crosscutting issues.Relequestual
Hopefully I'm not coming across as unhelpful. Please feel free to join the JSON Schema slack server from the official site if you'd like to discuss =]Relequestual
You've been very helpful actually, thanks! I'll probably stick with what I have so far, considering your words - when I was first introduced to JSON Schema, it was in the context of the tool adapting it for form generation, but I was under the impression it's a part of default functionality. Thanks again for clearing up that these things are really separate.Ivan T.

1 Answers

1
votes

This is not possible as of draft-7 (current at time of writing)