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

Help us to solve issues with validation #49

Closed
ratheesh-kr opened this issue Jul 15, 2013 · 11 comments
Closed

Help us to solve issues with validation #49

ratheesh-kr opened this issue Jul 15, 2013 · 11 comments

Comments

@ratheesh-kr
Copy link

When we validate our JSON against JSON Schema its get validated successfully.But in this cases we have noted two issues in your script
1,If we removed a mandatory key/value pair from json no error displayed ( Actually we need to display an error like " Key value pair is missing in json " like some online validators)
2,If we alter the key name no error should be raised.We need to raise some error like invalid JSON

@geraintluff
Copy link
Owner

Hi - could you please post an example schema, and some data that you believe should fail?

It'd be easier to tackle the problem if I have an example of the bug. :)

@ratheesh-kr
Copy link
Author

My sample JSON Schema and data as follows for validating

var schema ={
     "type":"object",
     "required":false,
     "properties":{
          "fieldElement": {
               "type":"object",
               "required":false,
               "properties":{
                    "readonly": {
                         "type":"boolean",
                         "required":true
                    },
                    "caption": {
                         "type":"string",
                         "required":false
                    }
               }
          },
          "fieldType": {
               "type":"string",
               "required":false
          }
     }
};

data =  {
            "fieldType": "text",
            "fieldElement": {
                "caption": "textfield",
                "readonly": true
          }
};

In this case this will work

Case 1

As per the Schema the 'readonly' property is a required field.

But When I removed the fieldElement ->readonly from data and try to validate, the result will be true.
The sample data as follows,

data =  {
            "fieldType": "textfield",
            "fieldElement": {
             "caption": "textfield"              
          }  
};

Case 2

As per the Schema the 'caption' is there as a property.

But When I changed the 'caption' to 'caption12345' in data and try to validate, the result will be true.

The sample data as follows,

data =  {
            "fieldType": "textfield",
            "fieldElement": {
                "caption12345": "textfield",
                "readonly": true
          }    
};

@geraintluff
Copy link
Owner

Ah - the issue is your use of "required". You are using the v3-style syntax.

In version 4 of the JSON Schema specification (which tv4 follows), the "required" keyword is an array in the parent object, not a boolean in the child.

Here is a modified version of your schema with the correct "required" syntax.

@ratheesh-kr
Copy link
Author

OK, thank you for your Advice :)
I need One more help from your side
I have created a JSON with the '$ref' variable
How can we validate the JSON schema with '$ref'.

@geraintluff
Copy link
Owner

The $ref in JSON Schema is only special when used in place of a schema. However, it has no special meaning when used in places like "properties":

{
    "type":"object",
    "properties":{
        "$ref": {
            "type":"string",
            "format": "uri"
        }
    }
}

With the above schema, it correctly checks the type of "$ref" in the data (example here).

@ratheesh-kr
Copy link
Author

Thanks for your Help,

But in my case the schema as follows

schema ={
     "type":"object",
     "required":false,
     "properties":{
          "fieldElement": {
               "type":"object",
               "required":false,
               "properties":{
                    "readonly": {
                         "type":"boolean",
                         "required":true
                    },
                    "caption": {
            "$ref": "../../../../common.schema.caption.json"    
            },
               }
          },
          "fieldType": {
               "type":"string",
               "required":false
          }
     }
};

The caption property had a ref path ( "../../../../common.schema.caption.json ) and the details for caption will take from that particular JSON Schema.
Can you please help me to solve this

Thanks
Ratheesh

@geraintluff
Copy link
Owner

Have you supplied that "common.schema.caption.json" schema to tv4?

If a $ref in a schema is not working, the first thing to check is tv4.missing. It is an array containing the URIs of all the $refs that could not be resolved.

After validation, what is the value of tv4.missing?

@ratheesh-kr
Copy link
Author

After validation the ref URLs comes in 'tv4.missing' array
eg: '../../../../common.schema.caption.json' is there in tv4 .missing.

Actually in ''../../../../common.schema.caption.json' which contains a json schema

{
"type":"string",
"required":false
}
We include this as a ref URL and which is not validating and the URL will be there in tv4.missing,
we just used the tv4.validate(data, schema) function

the schema contains schema with ref URL (common.schema.caption.json)

Can you please help us to solve this, Is this the right method? or Do i miss something?

@geraintluff
Copy link
Owner

Well, tv4 can't fetch files. Any schema that you use, you have to explicitly pass in.

Do you have a line like: tv4.addSchema('../../../../common.schema.caption.json', {..schema..})? If you don't, then tv4 has no way of accessing that schema.

@geraintluff
Copy link
Owner

Assuming closed.

@ratheesh-kr
Copy link
Author

Hi,
We have changed our JSON Schema to Version 4 and Corrected the validation using your library,
Thanks for your Help :)

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

2 participants