Skip to content

Enable $data reference usage for AJV validation #1255

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

Closed
1 task done
nitishdhar opened this issue Apr 16, 2019 · 7 comments
Closed
1 task done

Enable $data reference usage for AJV validation #1255

nitishdhar opened this issue Apr 16, 2019 · 7 comments

Comments

@nitishdhar
Copy link

Prerequisites

Description

Looking at the docs for AJV, it enables us to write validation rules using $data reference to an existing schema field's value, like

var ajv = new Ajv({$data: true});

var schema = {
  "properties": {
    "smaller": {
      "type": "number",
      "maximum": { "$data": "1/larger" }
    },
    "larger": { "type": "number" }
  }
};

var validData = {
  smaller: 5,
  larger: 7
};

ajv.validate(schema, validData); // true

But the default setup for AJV in RJSF does not enable $data=true

Is this an opinionated design choice for some reason or we can somehow enable it. I couldn't find documentation that lets us override the AJV setup.

Having the $data reference will enable us to write more complex schema based validations.

Is this something you will be open to enabling or letting users override this option somehow?

Thanks

@epicfaace
Copy link
Member

I'm open to enabling this! This library was initially created without AJV, so not all the possible features of AJV were enabled by default. Please make a PR and I'll take a look.

@nitishdhar
Copy link
Author

Thanks.

I will start working on a PR to enable $data reference.

Meanwhile, can you also share your thoughts on adding a new dependency to ajv-keywords.

This will enable the proposed keywords switch, patternRequired, formatMaximum / formatMinimum and formatExclusiveMaximum / formatExclusiveMinimum from JSON Schema extension proposals allowing us to do format based comparisons during validation. e.g. Defining schema validation to validate if a date should be minimum of another date attribute in the schema -

startDate: {
  type: "string",
  format: "date",
  title: "Start Date",
},
endDate: {
  type: "string",
  format: "date",
  title: "End Date",
  formatMinimum: {
    "$data": "1/startDate"
  }
}

If this makes sense, I will make this part of the PR.

@epicfaace
Copy link
Member

epicfaace commented Apr 17, 2019

Great, thanks!

Meanwhile, can you also share your thoughts on adding a new dependency to ajv-keywords.

Hmm, I'd be wary of adding in keywords that are not yet part of the JSON schema specification. For example, the ajv-keywords repository mentions that switch is deprecated and they suggest using if/then/else, which is part of the spec instead.

I'd be open to adding in functionality that is part of the JSON schema spec such as if/then/else (see #885 and #916), but otherwise it might take only a very good reason to add in additional keywords that are not part of the spec.

@epicfaace
Copy link
Member

epicfaace commented Jul 14, 2019

Actually, given that $data is not yet part of the JSON Schema spec (it's just a proposal as of now -- see json-schema-org/json-schema-spec#51), it might be better to wait until we see if and/or how it is incorporated into the JSON schema spec before implementing it in rjsf.

@charlysotelo
Copy link

I could use this feature pls

@epicfaace
Copy link
Member

Closing this issue -- can reopen if json-schema-org/json-schema-spec#51 is accepted

@zeddotes
Copy link

Hey, I've created a fix for this waiting to be approved by the team: #1668

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

No branches or pull requests

4 participants