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

Validate method #77

Open
simon-p-r opened this issue Jan 15, 2015 · 17 comments
Open

Validate method #77

simon-p-r opened this issue Jan 15, 2015 · 17 comments

Comments

@simon-p-r
Copy link
Contributor

Hi

When you use the validate method does it compile schemas too? If not what method do I need to use to compile schemas?

Thanks

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

As you see here: https://github.com/zaggino/z-schema/blob/master/src/ZSchema.js#L110-L141
validate method both compiles & validates the schema before it's used for validation of json.

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

There's a method validateSchema which you can use to compile & validate your schemas on server startup if you wish to do so: https://github.com/zaggino/z-schema/blob/master/src/ZSchema.js#L97-L109

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

This should be probably put into a README

@simon-p-r
Copy link
Contributor Author

Yes it would help green fingers like me, I am migrating a system to version 3 and our current use with 2 is the following.

During load up of our server we validate and then compile schemas (async)

However with version 3 I can use just the validate method to do both tasks, however how will I resolve the $ref flag? What does the 'ignoreUnresolvableReferences' option do? Will it resolve them if set to true or will it not validate that property on schema?

Best regards

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

ignoreUnresolvableReferences - this one will ignore validation of $schema keywords, not $ref, refs must be always resolved

Ideally you should load all schemas into an array and then pass this array into validateSchema method. It will compile and validate all schemas in that array and also resolve $refs

@simon-p-r
Copy link
Contributor Author

Ok I tried that but I am getting '__$refResolved' undefined. See one of the objects being returned by validate method

{ properties: 
   { ccySID: { type: 'string', format: 'sid', collection: 'currency' },
     altCcySubunit: { type: 'string' },
     numericCode: { type: 'integer' },
     smallestNote: 
      { '$ref': 'http://localhost:1234/amt',
        '__$refResolved': undefined },
     smallestTradedAmount: 
      { '$ref': 'http://localhost:1234/amt',
        '__$refResolved': undefined },
     category: { type: 'string', format: 'lookup', lutype: 'category' },
     displayOnWebsite: { type: 'boolean' },
     displayOnScreens: { type: 'boolean' },
     displaySequence: { type: 'integer' },
     isNotTraded: { type: 'boolean' },
     flag: 
      { '$ref': 'http://localhost:1234/image',
        '__$refResolved': undefined },
     control: 
      { '$ref': 'http://localhost:1234/control',
        '__$refResolved': undefined } },
  required: 
   [ 'sid',
     'altCcySubunit',
     'numericCode',
     'smallestNote',
     'smallestTradedAmount',
     'category',
     'displaySequence',
     'flag' ],
  title: 'collection schema for currency',
  additionalProperties: false,
  type: 'object' }

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

You need to validate schemas with given ids like http://localhost:1234/image before validating this schema (or put them all into an array)

@simon-p-r
Copy link
Contributor Author

Update I have tried removing ignoreUnresolvableReferences so values is false and have tried setting 'breakOnFirstError' to true however it doesn't break on first error and contains errors regarding $ref issues like this one

message: 'Reference could not be resolved: http://localhost:1234/control',

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

Do you have a schema with an id http://localhost:1234/control in the array you are trying to validate?

@simon-p-r
Copy link
Contributor Author

No

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

Then where'd you expect $ref http://localhost:1234/control to point to?

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

See this example from tests, notice the id and $ref elements

var schemas = [
                {
                    id: "personDetails",
                    type: "object",
                    properties: {
                        firstName: {
                            type: "string"
                        },
                        lastName: {
                            type: "string"
                        }
                    },
                    required: ["firstName", "lastName"]
                },
                {
                    id: "addressDetails",
                    type: "object",
                    properties: {
                        street: {
                            type: "string"
                        },
                        city: {
                            type: "string"
                        }
                    },
                    required: ["street", "city"]
                },
                {
                    id: "personWithAddress",
                    allOf: [
                        {
                            $ref: "personDetails"
                        },
                        {
                            $ref: "addressDetails"
                        }
                    ]
                }
            ];
validator.validateSchema(schemas);

@simon-p-r
Copy link
Contributor Author

Ok thank you, do I need to this as separate arrays for each of my schemas that are embedded?

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

You can put all of your schemas into one array and validator will take care of the rest. They don't even need to be in any specific order.

@simon-p-r
Copy link
Contributor Author

Ok thanks

@zaggino
Copy link
Owner

zaggino commented Jan 15, 2015

Leaving this open to update the README

@simon-p-r
Copy link
Contributor Author

Let me know if you need any help with documentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants