natesilva / jayschema

[Unmaintained] - A comprehensive JSON Schema validator for Node.js
BSD 3-Clause "New" or "Revised" License
217 stars 22 forks source link

Is the use of $ref in the JSON being validated supported? #24

Open ml019 opened 10 years ago

ml019 commented 10 years ago

I posted this question to a closed issue accidently so not sure if anyone saw it. I am using $ref to pull in multiple values into the JSON being validated.

I wanted to confirm if Jayschema supports the use of $ref in the input to be validated. The errors I am getting would suggest it doesn't or more likely, I've not got the code quite right.

The data file looks like this

{ "Profile":{ "Type":"ebMS3_PModeOperationSet", "Purpose":"Positive", "Title":"PMode Operation Set for Superstream Entry Level", "Description":"Covers ultra-light and light profiles", "Version":{ "Major":0, "Minor":1 }

}, "PModes" : [ {"$ref" : "http://test.compliancetest.net/get-profile?id=e7359f244623457286c672a4c1e7ae7dc8eb405b"}, {"$ref" : "http://test.compliancetest.net/get-profile?id=1f1c6bb0c31a2a5f78fc4757ad20f39e242b16ff"}, {"$ref" : "http://test.compliancetest.net/get-profile?id=92befc2c857eef0d0d912901dfedaa11e4269afe"} ] }

and the schema looks like this

{ "$schema" : "http://json-schema.org/draft-04/schema#", "title" : "ebMS3 PMode Operation Set Profile", "Version" : { "Major" : 0, "Minor" : 1 },

"type" : "object", "properties" : { "Profile" : {"$ref" : "#/definitions/Profile/Profile"}, "PModes" : {"$ref" : "#/definitions/PMode/OperationSet"}

}, "required" : ["Profile", "PModes"],

"definitions" : { "Types" : { "NonNegativeInteger" : {"type" : "integer", "minimum" : 0},

    "MultipleOf8"               : {"allOf": [{"$ref" : "#/definitions/Types/NonNegativeInteger"}, { "multipleOf": 8}]},

    "NonEmptyString"            : {"type" : "string", "minLength" : 1},

    "URI"                       : {"type" : "string", "format" : "uri", "minLength" : 1},

    "Boolean"                   : {"type": "boolean"},

    "BooleanDefaultFalse"       : {"allOf": [{"$ref" : "#/definitions/Types/Boolean"}, { "default": false }]},

    "BooleanDefaultTrue"        : {"allOf": [{"$ref" : "#/definitions/Types/Boolean"}, { "default": true }]},

    "TimeUnit"                  : {"enum" : ["seconds", "minutes", "hours", "days", "weeks"]}
},

"Profile" : {
    "Type"                      : {"enum" : ["ebMS3_PModeOperationSet"]},

    "Purpose"                   : {"enum" : ["Positive", "Negative"], "default" : "Positive"},

    "Version" : {
        "type" : "object",
        "properties" : {
            "Major"             : {"$ref" : "#/definitions/Types/NonNegativeInteger"},
            "Minor"             : {"$ref" : "#/definitions/Types/NonNegativeInteger"},
            "Patch"             : {"$ref" : "#/definitions/Types/NonNegativeInteger"}
        },
        "required" : ["Major", "Minor"]
    },

    "Profile" : {
        "type" : "object",
        "properties" : {
            "Type"              : {"$ref" : "#/definitions/Profile/Type"},
            "Purpose"           : {"$ref" : "#/definitions/Profile/Purpose"},
            "Title"             : {"$ref" : "#/definitions/Types/NonEmptyString"},
            "Description"       : {"$ref" : "#/definitions/Types/NonEmptyString"},
            "Version"           : {"$ref" : "#/definitions/Profile/Version"}
        },
        "required" : ["Type", "Purpose", "Title", "Description", "Version"]
    }
},

"PMode" : {
    "OperationSet" : {
        "type" : "array",
        "items"                 : {"$ref" : "https://test.compliancetest.net/?download_profile_type=1&type_id=26"},
        "minItems" : 1
    }
}

} }

the errors I am getting are

[ { instanceContext: '#/PModes/0', resolutionScope: 'https://test.compliancetest.net/?download_profile_type=1&type_id=26#', constraintName: 'required', constraintValue: [ 'Profile', 'General' ], desc: 'missing: Profile,General', kind: 'ObjectValidationError' }, { instanceContext: '#/PModes/1', resolutionScope: 'https://test.compliancetest.net/?download_profile_type=1&type_id=26#', constraintName: 'required', constraintValue: [ 'Profile', 'General' ], desc: 'missing: Profile,General', kind: 'ObjectValidationError' }, { instanceContext: '#/PModes/2', resolutionScope: 'https://test.compliancetest.net/?download_profile_type=1&type_id=26#', constraintName: 'required', constraintValue: [ 'Profile', 'General' ], desc: 'missing: Profile,General', kind: 'ObjectValidationError' } ]

Is there a way to see what the schema validator thinks is the effective JSON it is validating? I'm thinking my data includes aren't right for some reason.

Thanks for your assistance.

Regards Michael

natesilva commented 10 years ago

So if you are trying to use $ref to pull data into the instance (the data being validated), that won’t work. The $ref keyword is only for use in schemas. Just due to the way that JSON Schema works, the instance is expected to be plain old JSON and no special processing is done to it before validation.

ml019 commented 10 years ago

Thanks Nate, I figured that might be the case.

Is that a specific implementation choice you have made in JaySchema, or will all validators take that approach? As far as I can see from the JSON References RFC, its applicability isn't limited to use in JSON schemas.

As always, I am very appreciative of the prompt responses you provide.

Regards Michael

On Tue, Feb 4, 2014 at 4:08 AM, Nate Silva notifications@github.com wrote:

So if you are trying to use $ref to pull data into the instance (the data being validated), that won't work. The $ref keyword is only for use in schemas. Just due to the way that JSON Schema works, the instance is expected to be plain old JSON and no special processing is done to it before validation.

Reply to this email directly or view it on GitHubhttps://github.com/natesilva/jayschema/issues/24#issuecomment-33976026 .

natesilva commented 10 years ago

JSON-Ref and JSON Pointer were created by some of the same folks who created JSON Schema. I believe they were created for use in schemas, but you’re right, there’s nothing limiting it to that.

I don’t know if other validators allow you to have a $ref in your instance data. I believe most, if all, do not. It would be an interesting feature though.

ml019 commented 10 years ago

Thanks Nate.

My use-case naturally lent itself to $ref in the schema AND in the instance. Basically it is a situation where you want to aggregate existing objects which already have their own schemas, and validate the aggregation as a whole.

Any chance of adding $ref for instances to your feature wish list?

Thanks

Michael

From: Nate Silva [mailto:notifications@github.com] Sent: Wednesday, 5 February 2014 3:56 AM To: natesilva/jayschema Cc: ml019 Subject: Re: [jayschema] Is the use of $ref in the JSON being validated supported? (#24)

JSON-Ref and [JSON Pointer])[http://tools.ietf.org/html/rfc6901] were created by some of the same folks who created JSON Schema. I believe they were created for use in schemas, but you’re right, there’s nothing limiting it to that.

I don’t know if other validators allow you to have a $ref in your instance data. I believe most, if all, do not. It would be an interesting feature though.

— Reply to this email directly or view it on GitHub https://github.com/natesilva/jayschema/issues/24#issuecomment-34080834 .Image removed by sender.