Giter Club home page Giter Club logo

Comments (6)

zaggino avatar zaggino commented on August 10, 2024

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.

zaggino avatar zaggino commented on August 10, 2024

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.

kellyrmilligan avatar kellyrmilligan commented on August 10, 2024

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.

zaggino avatar zaggino commented on August 10, 2024

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.

kellyrmilligan avatar kellyrmilligan commented on August 10, 2024

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.

zaggino avatar zaggino commented on August 10, 2024

@kellyrmilligan callback is never fired issue has been fixed in 2.4.4

from z-schema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.