Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

final validation function and $refs #22

Closed
siddo420 opened this issue Jul 25, 2015 · 9 comments
Closed

final validation function and $refs #22

siddo420 opened this issue Jul 25, 2015 · 9 comments

Comments

@siddo420
Copy link

I have two feature requests that may not be standards based but I think they'll be really useful:

1- An ajv function/method that can be provided with input data and returns TRUE or FALSE based on the input data as a first/final check, since there still are some things missing from JSON schema (or perhaps impossible the way I've written the schemas now).

2- A function that'd print the final JSON schema after replacing all $refs with actual content (ie. referenced schemas).

You can print warnings etc when they are used, so users know what they are doing.

@siddo420
Copy link
Author

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

@epoberezkin
Copy link
Member

  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.

@siddo420
Copy link
Author

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?

@epoberezkin
Copy link
Member

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?

@epoberezkin
Copy link
Member

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

@siddo420
Copy link
Author

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).

@epoberezkin
Copy link
Member

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.

@epoberezkin
Copy link
Member

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

@RS-Roshi
Copy link

RS-Roshi commented Dec 5, 2018

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.
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants