7

I need to add an optional property to a JSON schema. This property is of Enum type. I need to set default value in the case the user does not specify this field.

// schema
"properties" : {
    "Param" : {
        "type" : "string",
        "enum" : [ " p1", "p2" ],
        "optional" : true,
        "default" : "p2",
        "required" : true
    }
}

If user will not specify "Param" field it should recognize field as "p2"

4 Answers 4

7

Add null to the enum array

More: https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values

"properties" : {
    "Param" : {
        "type" : "string",
        "enum" : [ " p1", "p2", null ], // <--
        "default" : "p2", // <--
        "required" : true
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not an answer, it is just a code block. Please provide some text to support your answer
unfortunate side effect that null now is a valid enum. probably not what OP wanted.
3

As you have put in your example, "default" is a valid json-schema keyword. But its use is up to the schema consumer.

Take into account that json-schema is concerned with data structure definition and validation. In fact this keyword was added after much discussion because it is so common that we want to give a hint to clients of what should be a default value in case they do not want to set one. But, again, it is up to the client to make use this value or not.

Another way to approach your particular case would be to use "oneOf" splitting enum values.

"required" : ["Param"],
"oneOf" : [{
        "properties" : {
            "Param" : {
                "enum" : ["p2"]
            }
        }
    }, {
        "properties" : {
            "Param" : {
                "enum" : ["p1", "p3"]
            }
        }
    }
]

In this case you are telling the client: "at least you must send me "Param" with value "p2".

Finally, you could also add a pre-process step in your server side where you take all missing properties with default value, and add them to json message before validation.

2 Comments

thanks! as i have many properties 'OneOf' is not appropriate for me.
Many enum properties with default values? Then your unique option is to make a post-process step in the client or a pre-process one in the server. Not a big deal.
0

The solution is not in the schema but in the parser/compiler; unspecified fields should have the value 0 when transferred to variable.

In this case it would be:

   "enum" : [ "p2", "p1" ],

and the equivalent in C would be:

enum {
 p2 = 0,
 p1 = 1
}

Hope this help.

Comments

-2
"properties" : {
    "Param" : {
        "type" : "string",
        "enum" : ["p1", "p2"],
        "default" : "p2"
    }
},
"required" : ["Param"]

1 Comment

This solution makes Param required. The OP wants the enum to be optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.