Comments (9)
For example, in the second case above, I should be able to do the following:
try{
ajv.addSchema( schema, 'schema.json' );
console.log( ajv.printSchema( 'schema.json' )) ; // printSchema or getSchema whatever
}
catch(e){console.log(e);}
from ajv.
- Can you explain more or maybe provide some example? Sorry, I didn't understand.
- The second idea is interesting. I was thinking more about inlining compiled refs rather than generating schema. It won't work with recursive refs though.
from ajv.
Here is one way of doing it (but you can come up with a better approach:
ajv.addSchema( schema, 'schema.json', /*optional*/ function( input ){
// check for additional input data validity (anything not covered by json schema validation already)
if( input is valid ) return true;
else return false;
});
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?
from ajv.
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:
function compile(schema, finalCheck) {
var v = ajv.compile(schema);
return function validate(json) {
var valid = v(json);
if (valid) {
valid = finalCheck(schema, json);
validate.errors = valid ? null : [{ /**/ }];
} else {
validate.errors = v.errors;
}
return valid;
};
}
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?
from ajv.
By the way, custom formats may be helpful, although at the moment they only validate strings.
from ajv.
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).
from ajv.
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.
from ajv.
Closing. Generating schema with substituted references won't help as this schema is not used for validation anyway.
from ajv.
I have found a possible way to get compiled schema with resolved references.. maybe it could help.
//Using Angular 7 Syntax
//parent schema-- must follow id#/JSON Pointer even to resolve reference from same file as below for "loc"
schema: any = {
"$id": "schema.json",
"type": "object",
"properties": {
"foo": { "$ref": "defs.json#/definitions/int" },
"hel": { "$ref": "defs.json#/definitions/int" },
"bee": { "$ref": "defs.json#/definitions/int" },
"loc": { "$ref": "schema.json#/definitions/str" },
"bar": { "$ref": "defs.json#/definitions/str" }
},
"required": ["foo"]
};
//child schema
defsSchema: any = {
"$id": "defs.json",
"definitions": {
"int": { "type": "integer", "title": "Hello" },
"str": { "type": "string" }
}
};
//ajv instance
ajv: any = new Ajv({schemas: [this.schema, this.defsSchema]});
//on intialize function because i want to console the output as the web intialize
ngOnInit(){
this.schema = this.ajv.getSchema('schema.json'); //output in validate function that has same schema
this.dereferencedObj = this.dereference(this.schema.schema);//calling the dereference function and setting its returened value
console.log(this.dereferencedObj);
}
//function that will dereference the schema object and return you the compile schema object
dereference(obj, id = null) {
if(id == null) {
id = obj.$id
}
//lodash function to separate keys and values of object
_.forOwn(obj, (value, key, obj) => {
if(value.$ref) {
let schemaRef //store the $ref value
if(_.startsWith(value.$ref, '#')) {
schemaRef = id + value.$ref.substr(1) //for reference in same schema
} else {
schemaRef = value.$ref // when reference is external
}
obj[key] = this.ajv.getSchema(schemaRef).schema //set the resolve value to the obj[key]
}
if(_.isObject(value)) {
this.dereference(value, id)
}
})
return obj //return compiled schema with resolved references.
}
from ajv.
Related Issues (20)
- Contains object property with unknown key matching a schema HOT 1
- Distinction between addVocabulary and addKeyword in the docs
- Documentation dead links HOT 1
- Toss a Warning instead of throwing an error when adding a keyword HOT 6
- Ref within def does not act consistently
- JSONSchemaType $ref doesn't work in array type. HOT 5
- oneOf returns unwanted errors HOT 1
- Description key set as boolean returns error that it must be string HOT 2
- Validate and infer JSON Schema validated data type: introducing type providers HOT 3
- JTD schema parser errors on empty properties field HOT 1
- Implement x-inOrder subschema for in order or sequential processing HOT 3
- additionalProperties shouldn't affect TypeScript types from JTDDataType HOT 2
- option compile from addKeyword doesn't change on a remove and then add again
- OneOf validation issue HOT 1
- Cannot find module 'ajv/dist/compile/codegen' HOT 2
- How can I make a property value depend on the value of another property? HOT 1
- Error using multipleOf with decimals HOT 1
- Using if/then/else fails validation HOT 1
- compileParser does not appear to support properties with type `field?: Record<string, unknown>` via `JTDSchemaType`. HOT 2
- Integers as Discriminators HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ajv.