How to parse true as boolean in json schema?

82 Views Asked by At

How can I parse the following schema using NJsonSchema? Is it valid to use "default": true?

    var problemJsonSchema = """
{
    "title": "JSONSchema",
    "default": {},
    "oneOf": [{
            "$ref": "#/definitions/JSONSchemaObject"
        },
        {
            "$ref": "#/definitions/JSONSchemaBoolean"
        }
    ],
    "definitions": {
        "JSONSchemaBoolean": {
            "title": "JSONSchemaBoolean",
            "description": "Always valid if true. Never valid if false. Is constant.",
            "type": "boolean"
        },
        "JSONSchemaObject": {
            "title": "JSONSchemaObject",
            "type": "object",
            "properties": {
                "$id": {
                    "type": "string"
                },
                "default": true
            }
        }
    }
}
    """;

    var metaSchema = JsonSchema.FromJsonAsync(problemJsonSchema).Result;

This code throws with error,

One or more errors occurred. (Unexpected initial token 'Boolean' when populating object. Expected JSON object or array. Path 'definitions.JSONSchemaObject.properties.default', line 21, position 27.)

1

There are 1 best solutions below

3
ggeorge On

You need to define the type of the property (like you did with id) and a default value as true.

var problemJsonSchema =
"""
{
    "title": "JSONSchema",
    "default": {},
    "oneOf": [
        {
            "$ref": "#/definitions/JSONSchemaObject"
        },
        {
            "$ref": "#/definitions/JSONSchemaBoolean"
        }
    ],
    "definitions": {
        "JSONSchemaBoolean": {
            "title": "JSONSchemaBoolean",
            "description": "Always valid if true. Never valid if false. Is constant.",
            "type": "Boolean"
        },
        "JSONSchemaObject": {
            "title": "JSONSchemaObject",
            "type": "object",
            "properties": {
                "$id": {
                    "type": "string"
                },
                 "default": {
                     "type": "boolean",
                     "default": true
                 }          
            }
        }
    }
}
""";