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

Question: Anyway to get a "compiled" version of the schema? #125

Closed
conrad10781 opened this issue Feb 17, 2016 · 11 comments
Closed

Question: Anyway to get a "compiled" version of the schema? #125

conrad10781 opened this issue Feb 17, 2016 · 11 comments

Comments

@conrad10781
Copy link

First, let me thank you for the work on the validator. We're using it to satisfy quite a few needs we've had with data validation on older data sets we have.

Is there anyway to get a "compiled" version of the schema that is built with $ref's? We need to pass it to a 3rd party utility we don't have control over and this utility doesn't seem to support $ref's in any way we've been able to figure out.

Specifically, where you would normally have the following ( I trimmed it down for readability ):

Product

{
    "type": "object",
    "id": "#Product",
    "properties": {
        "label": { 
            "type": "string"
        },
        "category": { 
            "$ref": "Category"
        }
    },
    "required": [
        "label",
        "category"
    ],
    "additionalProperties": false
}

Category

{
    "type": "object",
    "id": "#Category",
    "properties": {
        "label": { 
            "type": "string"
    },
    "required": [
        "label"
    ],
    "additionalProperties": false
}

Is there anyway to execute something so that we could get something similar to the following? I'm sorry if "compiled" is not the right term.

{
    "type": "object",
    "id": "#Product",
    "properties": {
        "label": { 
            "type": "string"
        },
        "category": { 
            "type": "object",
            "properties": {
                "label": { 
                    "type": "string"
                }
            },
            "required": [
                "label"
            ],
            "additionalProperties": false
        }
    },
    "required": [
        "label",
        "category"
    ],
    "additionalProperties": false
}
@epoberezkin
Copy link
Member

ajv.compile(schema) returns validation function that can be used to validate data (see example in readme), regardless whether it uses refs or not.

From your example you seem to want a schema with refs merged in... Ajv doesn't produce such thing, refs are either inlined as code in generated validation function (if they don't contain other refs and if they are not recursive) or compiled as separate functions. The reason this "final" schema is not created is that schema can be recursive (and it is a very common thing) in which case it is impossible to create.

This seems to be similar to #22.

@epoberezkin
Copy link
Member

what is the utility by the way?

@epoberezkin
Copy link
Member

I was thinking what would I do if I had such an issue... I would probably simply use some templates (doT templates in my case :) to generate two different schemas - one with refs, another without.

Another alternative is to use code to merge them as objects... It's not that difficult to write a generic processor that would replace $refs with actual schemas in case it is not recursive - you can even make a schema with a custom keyword to do such processing:

var processingSchema = {
  properties: {
    $ref: {
      replaceRef: true
    }
  },
  additionalProperties: { $ref: '#' },
  items: { $ref: '#' }
}

All you need to do is to define a custom keyword replaceRef (that will resolve and replace the ref with object) and then pass your schema as data to this "processing" schema:

ajv.addKeyword('replaceRef', { ... });
ajv.validate(processingSchema, yourSchemaYouWantToMergeTheRefsIn);

At the moment you can only do it with inline keyword (as you need access to the parent object), but the next release which I am about to make will allow to use simpler validation/compiled keywords to do so.

@conrad10781
Copy link
Author

Thank you for the prompt response.

"what is the utility by the way?"
^-- It is a no-name custom utility that is a provided by a small company to
handle the "shuttling" of data from our platform to another. It's just big
enough where it'd be annoying to write something to replace it to get the
validation working as intended.

Regarding the generic processor, that is the approach we were going to go
provided ajv didn't do this. Just didn't want to spend any time doing that
if something was already available in ajv.

Thank you again for your time and the prompt response. We will move forward
with the pre-processor approach.

On Wed, Feb 17, 2016 at 1:14 PM, Evgeny Poberezkin <[email protected]

wrote:

I was thinking what would I do if I had such an issue... I would probably
simply use some templates (doT templates
http://olado.github.io/doT/index.html in my case :) to generate two
different schemas - one with refs, another without.

Another alternative is to use code to merge them as objects... It's not
that difficult to write a generic processor that would replace $refs with
actual schemas in case it is not recursive - you can even make a schema
with a custom keyword to do such processing:

var processingSchema = {
properties: {
$ref: {
replaceRef: true
}
},
additionalProperties: { $ref: '#' },
items: { $ref: '#' }
}

All you need to do is to define a custom keyword replaceRef (that will
resolve and replace the ref with object) and then pass your schema as data
to this "processing" schema:

ajv.addKeyword('replaceRef', { ... });
ajv.validate(processingSchema, yourSchemaYouWantToMergeTheRefsIn);

At the moment you can only do it with inline keyword (as you need access
to the parent object), but the next release which I am about to make will
allow to use simpler validation/compiled keywords to do so.


Reply to this email directly or view it on GitHub
#125 (comment).

Matthew C. Rice
RCS
http://rcs.us
[email protected]
TF - 844-4-OLAINC Ext. 700
TF - 844-465-2462 Ext. 700

@ro70
Copy link

ro70 commented Jul 11, 2018

@conrad10781 Have you found a solution for the processing approach as @epoberezkin is proposing?

@conrad10781
Copy link
Author

@ro70 , it was some time ago, but we ended up just iterating through all the schema's first, so we had them all, then iterating again and replacing ref instances with the actual schema.

@ro70
Copy link

ro70 commented Jul 12, 2018

@conrad10781 Ok, I implemented something similar to your solution. My schemas had to be dereferenced and demerged. Thank you.

@jegj
Copy link

jegj commented Jul 30, 2018

@ro70 could you please share your solution ?

@ro70
Copy link

ro70 commented Jul 30, 2018

@jegj For my purpose the following code did it. My schemas each have an id like "$id": "User.json#". I use _lodash functions.

function dereference(obj, id = null) {
  if(id == null) {
    id = obj.$id
  }
  _.forOwn(obj, (value, key, obj) => {
    if(value.$ref) {
      let schemaRef
      if(_.startsWith(value.$ref, '#')) {
        schemaRef = id + value.$ref.substr(1)
      } else {
        schemaRef = value.$ref
      }
      obj[key] = ajv.getSchema(schemaRef).schema
    }
    if(_.isObject(value)) {
      dereference(value, id)
    }
  })
  return obj
}

let schema = ajv.getSchema(`${model}.json#`)
let dereferencedObj = dereference(schema.schema)

@jegj
Copy link

jegj commented Jul 30, 2018

thanks !

@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
Development

No branches or pull requests

5 participants