Comments (6)
Hi @kellyrmilligan
You should load all the schemas into an array (z-schema doesn't care about the order) and then compile them in a batch. This is a sample scenario which will work, note the $ref
and id
attributes.
var schemaA = {id: "schemaA", type: "integer"};
var schemaB = {id: "schemaB", type: "string"};
var mainSchema = {
id: "mainSchema",
type: "object",
properties: {
a: {"$ref": "schemaA"},
b: {"$ref": "schemaB"},
c: {"enum": ["C"]}
}
};
var validator = new ZSchema();
validator.compileSchema([schemaA, schemaB, mainSchema]).then(function () {
return validator.validate({a: 1, b: "b", c: "C"}, mainSchema).then(function (report) {
assert.isTrue(report.valid);
done();
});
})
More examples here:
https://github.com/zaggino/z-schema/blob/master/test/issue_13.js
https://github.com/zaggino/z-schema/blob/master/test/issue_23.js
from z-schema.
To your original question, there's no mechanism (and there should not be) for z-schema to search your hard-drive for other schemas which you will not feed into compile step. If you want to load all files from a certain folder, use something like this:
function readDirToObject(dirpath) {
var arr = [];
fs.readdirSync(dirpath).forEach(function (fileName) {
arr.push(JSON.parse(fs.readFileSync(dirpath + fileName, 'utf8')));
});
return arr;
}
from z-schema.
Hi, I have read in all the files that are in the directories with my main json file, and sent them to compile schema as you have suggested. i'm currently using callback style, and neither err or compiledSchema arguments have anything in them when I send an array of json objects to it. should they be parsed or just strings?
from z-schema.
If err
is empty it probably passed.
To be 100% sure, just check if your schema objects have __$compiled
and __$validated
attributes added to them, if yes, everything is fine and you can use them to validate your objects.
If you want, feel free to email me your samples on zaggino at gmail
from z-schema.
hi, it looks like a few things are happening, see code below:
validator.compileSchema(schemas, function (err, compiledSchema) {
if (err) {
console.log(err);
} else {
console.log(compiledSchema);
}
});
validator.compileSchema(schemas)
.then(function (data) {
console.log(data);
})
.catch(function(err){
console.log(err);
});
When done with promises, the err object is showing me the issue, but in callback style, the callback is never fired.however, if I send the parsed contents of 1 schema file, callback style is working just fine. it looks like if I send an array, only if I do it with promises is it working.
Secondly, and I'll send some samples in an email, I am back to the original issue.
errors:
[ { code: 'UNRESOLVABLE_REFERENCE',
message: 'Reference could not be resolved: error.json',
path: '#/',
params: [Object] },
{ code: 'UNRESOLVABLE_REFERENCE',
message: 'Reference could not be resolved: application.json',
path: '#/',
params: [Object] },
{ code: 'UNRESOLVABLE_REFERENCE',
message: 'Reference could not be resolved: document.json',
path: '#/',
params: [Object] },
{ code: 'UNRESOLVABLE_REFERENCE',
message: 'Reference could not be resolved: application-status.json',
path: '#/',
params: [Object] },
{ code: 'UNRESOLVABLE_REFERENCE',
message: 'Reference could not be resolved: eligibility-request.json',
path: '#/',
params: [Object] },
{ code: 'UNRESOLVABLE_REFERENCE',
message: 'Reference could not be resolved: eligibility-request.json',
path: '#/',
params: [Object] } ],
but all of these are in the array of files I send to compile schema call. any ideas?
from z-schema.
@kellyrmilligan callback is never fired
issue has been fixed in 2.4.4
from z-schema.
Related Issues (20)
- add keyword in error objects of getLastErrors()
- Allow to specify errors to be reported
- why when schema type is string, there is no error when the value type is integer or number ? HOT 2
- Update to core-js 3.x
- Doesn't validate minimum and maximum on numbers HOT 1
- Versions mismatch cause npm error HOT 4
- Remove core-js HOT 2
- Missing type for Validator.validate(json, schema, options) HOT 3
- 'email' format accepts email addresses with several types of whitespace. HOT 3
- 'ARRAY_ADDITIONAL_ITEMS' error unreachable?
- Is there any approach to suppress an error code HOT 2
- Local schemas not being retrieved from cache
- zSchema doesn't support json-schema post 2020-12
- Vulnerabilities introduced by package validator HOT 2
- Vulnerability in Validator JS HOT 1
- Update Validator to 13.7.0 HOT 1
- `lastReport` absent in `index.d.ts` HOT 2
- Update commander as it's version marked as EOL
- Support for Unicode Properties
- List on json-schema.org HOT 1
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 z-schema.