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

Extending properties with $ref #260

Closed
gobbledygook88 opened this issue Aug 2, 2016 · 11 comments
Closed

Extending properties with $ref #260

gobbledygook88 opened this issue Aug 2, 2016 · 11 comments

Comments

@gobbledygook88
Copy link

Considering the following schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Schema",
  "type": "object",
  "definitions": {
    "ref": {
      "type": "integer"
    }
  },
  "properties": {
      "prop": {
          "$ref": "#/definitions/ref",
          "minimum": 10
      },
      "propWithAllOf": {
          "allOf": [{
            "$ref": "#/definitions/ref",
          }, {
            "minimum": 10
          }]
      }
  }
}

I currently get the following results when validating against a range of input objects

Input Object Valid
{ "prop": 10 } true
{ "prop": 1 } false
{ "propWithAllOf": 10 } true
{ "propWithAllOf": 1 } false

According to the JSON spec

Any members other than "$ref" in a JSON Reference object SHALL be ignored.

Does that mean the second result should be true? Thanks!

@epoberezkin
Copy link
Member

epoberezkin commented Aug 2, 2016

You are right.

Ajv doesn't comply with this provision, it allows extending $refs with additional keywords.
I was going to address it in the next major version change.
I was also considering adding the option in the current version that would allow to choose between:

  • failing schema compilation (I think it should be the default to be honest)
  • ignoring extra keywords (as per spec, should log warning)
  • using extra keywords (current behaviour).

In this case the 1st or 2nd option could become the default in the next major version.

@mbroadst
Copy link
Contributor

@epoberezkin can you expand on what we need to change here? I'm getting this warning now and it pointed me here but I'm not quite sure what the implications are.

I'm currently using $ref to reference a definition which is a oneOf a number of schemas with custom keywords in it - are you saying this will not be possible in the upcoming release?

@mbroadst
Copy link
Contributor

mbroadst commented Aug 12, 2016

Oh. I think this is a false positive for my case, after reading this more closely. My schema looks like:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Schema",
  "type": "object",
  "definitions": {
    "date": {
      "oneOf": [
        { "instanceOf": "Date" },
        { "type": "string", "coerceTo": "Date" },
        { "type": "number", "coerceTo": "Date" }
      ]
    }
  },
  "properties": {
      "prop": { "$ref": "#/definitions/date" }
  }
}

"coerceTo" and "instanceOf" are custom keywords, but as far as the definition goes there is no other keyword other than $ref in my definition - yet the warning is popping up

@epoberezkin
Copy link
Member

Could you please submit a new issue - it may be a bug

@mbroadst
Copy link
Contributor

ah! apologies after some deep diving I discovered I am indeed auto-generating some schema bits that use a custom keyword with $ref, is that not going to be possible in the next major version?

@epoberezkin
Copy link
Member

There will be an option to preserve the current behaviour.
By spec everything but $ref should be ignored, the correct way to extend $ref is to use allOf keyword.

mbroadst added a commit to mbroadst/thinkagain that referenced this issue Aug 14, 2016
@epoberezkin
Copy link
Member

in 4.4.0

@emarkovskyi
Copy link

emarkovskyi commented Dec 20, 2018

You are right.

Ajv doesn't comply with this provision, it allows extending $refs with additional keywords.
I was going to address it in the next major version change.
I was also considering adding the option in the current version that would allow to choose between:

  • failing schema compilation (I think it should be the default to be honest)
  • ignoring extra keywords (as per spec, should log warning)
  • using extra keywords (current behaviour).

In this case the 1st or 2nd option could become the default in the next major version.

Dear @epoberezkin first of all thank you for so nice and powerful project,
we find weird issue wrt extending of $ref (BTW such extending is a great fetaure)
We trying to create some base schema inside definition and also extend this schema by extra fields inside the same definitions, e.g.

 {
    '$schema': 'http://json-schema.org/draft-07/schema',
    '$id': 'http://www.mydomain.com/definitions.json',
    'type': 'object',
    'definitions': {
        "basic-definition": {
         },
        "extended-basic-definition": {
          //reference to the basic, but extra properties are ignored
          "$ref": "http://www.mydomain.com/definitions.json#/definitions/basic-definition"
           "properties": {
               extra properties....
            }
        }
     }
  } 

and AJV in this case just ignore extra filed and uses on properties from the basic.
Could we achieve such extending somehow?
If $ref in properties e.g (below) everything works like a charm

 {
    '$schema': 'http://json-schema.org/draft-07/schema',
    '$id': 'http://www.mydomain.com/definitions.json',
    'type': 'object',
    'definitions': {
        "basic-definition": {
         },
        "extended-basic-definition": {          
           "properties": {
              "my-property": {
                  **//reference to the basic, works great**
                   "$ref": "http://www.mydomain.com/definitions.json#/definitions/basic-definition"
                    "properties": {
                         extra properties....
                     }
               }
            }
        }
     }
} 

Thank you in advanced.

@epoberezkin
Copy link
Member

Ajv now ignores all siblings to $ref but it can be changed with options. See the docs.

@emarkovskyi
Copy link

emarkovskyi commented Dec 21, 2018

Ajv now ignores all siblings to $ref but it can be changed with options. See the docs.

@epoberezkin, thank you for quick response, so TBH quite difficult to find correct option :-), I tried to play with extendRefs: true, inlineRefs: false}
What option do you mean? Maybe it could be a specific option of add/compileSchema methods?
Thank you in advanced.

@emarkovskyi
Copy link

emarkovskyi commented Jan 19, 2019

Ajv now ignores all siblings to $ref but it can be changed with options. See the docs.

@epoberezkin we still could not find proper options, tend to think that hierarchy described above (schema extending inside definitions) is impossible for now, but could you please give us a hint what options do you mean?
Thank you in advanced.

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

No branches or pull requests

4 participants