Giter Club home page Giter Club logo

Comments (9)

zaggino avatar zaggino commented on August 10, 2024

Interesting, do you have a quick sample of how this is done with others? I don't think it's possible right now without urls but it would not make much of an effort to implement this.

from z-schema.

k7sleeper avatar k7sleeper commented on August 10, 2024

Well, here is a piece of production code using JSV:

var jsv = (require('jsv')).JSV;
var jsonSourceSchemas = (require('require-all'))({
    dirname: path.join(__dirname, 'schema-files'),
    filter: /(.+)\.json$/,
    excludeDirs: /^\.(git|svn)$/
  });

var jsvEnv = jsv.createEnvironment();
var mainSchema;

for (schemaName in jsonSourceSchemas) {
    schemaJson = jsonSourceSchemas[schemaName];
    sc = jsvEnv.createSchema(schemaJson);
    if (schemaName == "mainSchema") {
        mainSchema = sc
    }
}

var validateRequest = function(req) {
    valrep = jsvEnv.validate(req, mainSchema);
    if (valrep.errors.length) {
      throw new Error("JSON Schema validation failed for incoming request!");
}

With JaySchema it should be possible similarly. With JaySchema you can call getMissingSchemas() after each register() call which I find very useful.

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

Ok, I'll definitely think of something ;-)

from z-schema.

k7sleeper avatar k7sleeper commented on August 10, 2024

I'm curious about it ...

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

I have this API working in 2.1.0, what do you think? I'd like a feedback before I close this or code something more on top of it.

     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();
         });
     })

You can push as many schemas as you want into compile, just the order has to be correct as you can't compile mainSchema before schemaA and schemaB are compiled. Validator remembers schemas it compiled so you don't have to compile everything in one go.

from z-schema.

k7sleeper avatar k7sleeper commented on August 10, 2024

API is ok with the exception of "the order has to be correct". It's quite a complex task to ensure the order of the schemas in the list if you just read the schemas from a folder. Because I don't know anything about the schemas (except that there exists a main schema), to ensure the correct order of the schemas, I have to scan all attributes of all schemas, check whether it's a ref, search that ref in the other schemas ... That's a task that I don't expect do have been done by a library user.

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

Yes, you have a point there ... I'll try to think of something.

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

@k7sleeper "the order has to be correct" is now fixed in 2.2.1.
You can test if you want. I'd land it sooner but I was on holiday ;-)

from z-schema.

k7sleeper avatar k7sleeper commented on August 10, 2024

Thanks, it works as expected for me!

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.