-
-
Notifications
You must be signed in to change notification settings - Fork 878
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
final validation function and $refs #22
Comments
For example, in the second case above, I should be able to do the following:
|
|
Here is one way of doing it (but you can come up with a better approach:
Whenever, ajv.validate is called on this schema then this function will be called with the provided input for any additional checks on input data. Makes sense? |
I understand now. I don't think it should be the part of library, not because of the standards, but because I think it is not a very common use case. You can create a simple wrapper that would do it:
The projects we use ajv in have project specific wrappers that add features that are needed for them. I am thinking about the second suggestion. I was wondering, what is your use case for which you need schema with substituted refs? |
By the way, custom formats may be helpful, although at the moment they only validate strings. |
I recently started using JSON schema and ajv, so my use case was debugging :) I wanted to make sure refs are replaced with proper values and how final schema looks like I don't think formats would work in my case. I want to make sure a certain key is present (possibly with certain values) based on certain values of another key (a weird/complicated case of dependency, I guess). |
Ajv doesn't generate a final schema. Each ref is compiled as a separate function - that makes recursion possible and also makes refs reusable between schemas referring to them. It doesn't mean that generating schema with refs substitution is impossible, but it is not available anywhere internally at the moment. To make sure that your schemas work correctly you could have tests with valid/invalid data samples. You can use https://github.com/MailOnline/json-schema-test - it is used to test ajv and we use it to test schemas in our project too. It allows you defining your test cases as json files that contain schemas and data samples (or references to files with them). I will think about custom validation. At the moment I think it is out of scope of json-schema validation, because attempting to include any custom validation in schemas would make them incompatible with other platforms. So I think it is better to have it as a separate validation step, additional to json-schema validation. In your case you can try using "dependencies" and "anyOf" keywords and maybe consider changing data structure (if possible) so that different data is not kept in the same fields. |
Closing. Generating schema with substituted references won't help as this schema is not used for validation anyway. |
I have found a possible way to get compiled schema with resolved references.. maybe it could help.
|
I have two feature requests that may not be standards based but I think they'll be really useful:
1- An ajv function/method that can be provided with input data and returns TRUE or FALSE based on the input data as a first/final check, since there still are some things missing from JSON schema (or perhaps impossible the way I've written the schemas now).
2- A function that'd print the final JSON schema after replacing all $refs with actual content (ie. referenced schemas).
You can print warnings etc when they are used, so users know what they are doing.
The text was updated successfully, but these errors were encountered: