Skip to content
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

"default" is not working inside $ref #506

Open
HemaSudha1498 opened this issue Mar 28, 2024 · 0 comments
Open

"default" is not working inside $ref #506

HemaSudha1498 opened this issue Mar 28, 2024 · 0 comments

Comments

@HemaSudha1498
Copy link

In the below example, The footerHeight is not resolved, and the default value is not set. I have approximately 50-70 properties of the same kind, all of which require default values, but duplicating the schema is not feasible.

My Schema:

{
    "$schema": "https://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "templateName": {
            "type": "string",
            "minLength": 2,
            "maxLength": 25
        },
        "headerHeight": {
            "type":"object",
            "additionalProperties": false,
            "default": {
                "units": "px",
                "value": 0
            },
            "properties": {
                "value": {
                    "type": "number"
                },
                "units": {
                    "type": "string",
                    "enum": ["px", "%"]
                }
            }
        },
        "footerHeight": {
            "$ref": "#/definitions/dimensionWithDefault"
        }
    },
    "definitions": {
        "dimensionWithDefault": {
            "type": "object",
            "additionalProperties": false,
            "default":{
                "value": 0,
                "units": "px"
            },
            "properties": {
                "value": {
                    "type": "number"
                },
                "units": {
                    "type": "string",
                    "enum": ["px", "%"]
                }
            }
        }
    },
    "required": ["templateName"]
}

My Code:

public class JsonExample {
    public static void main(String[] args) {
        JSONObject` input = new JSONObject("{\n" +
                "    \"templateName\": \"xyz\"\n" +
                "}");
        Schema schema = SchemaLoader.builder()
                .useDefaults(true)
                .schemaJson(rawSchema)
                .build()
                .load().build();

        schema.validate(input);
        System.out.println(input);
    }
}

The Output:

{
    "templateName": "xyz",
    "headerHeight": {
        "units": "px",
        "value": 0
    }
}

Kindly provide guidance on resolving this issue.

Thanks

@HemaSudha1498 HemaSudha1498 changed the title "default" does not work inside $ref "default" is not working inside $ref Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant