Giter VIP home page Giter VIP logo

Comments (9)

siddo420 avatar siddo420 commented on May 12, 2024

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.

epoberezkin avatar epoberezkin commented on May 12, 2024
  1. Can you explain more or maybe provide some example? Sorry, I didn't understand.
  2. 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.

siddo420 avatar siddo420 commented on May 12, 2024

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.

epoberezkin avatar epoberezkin commented on May 12, 2024

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.

epoberezkin avatar epoberezkin commented on May 12, 2024

By the way, custom formats may be helpful, although at the moment they only validate strings.

from ajv.

siddo420 avatar siddo420 commented on May 12, 2024

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.

epoberezkin avatar epoberezkin commented on May 12, 2024

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.

epoberezkin avatar epoberezkin commented on May 12, 2024

Closing. Generating schema with substituted references won't help as this schema is not used for validation anyway.

from ajv.

RS-Roshi avatar RS-Roshi commented on May 12, 2024

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)

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.