Skip to content

Proposal: schema primitives in ReQL #5879

Open

Description

@marshall007

I'd like to propose a set of utility functions for creating schema validators that I think would be useful in a variety of contexts.

Goals

  1. Provide rich ReQL API for defining and executing object schemas.
  2. Meet needs of Horizon validation functions so they can be run in ReQL.
  3. Identify and support common validation operators.
    • Long term I think we should err on the side of being feature-rich.
  4. Use as secondary index functions? (i.e. indexCreate(..., r.schema(...)))

Possible Future Goals

  1. Assign schema to a table and enforce on all write operations.
  2. Explicit schema optarg to insert, update, etc.
  3. Use as document expiration functions from Proposal: Document expiration #5823 (i.e. expire when document becomes invalid).

Questions

  • Should we instead rely on or at least support parsing some standard format like JSON Schema?
  • Do or will we ever need to worry about validating document relationships?

Usage

r.schema(<schema>)(<data>, <options>)

r.schema(...) returns a function that accepts two arguments, the context to evaluate the schema in and any options we might support. Executing the function returns an object containing the validation status and either the valid data or any validation errors that occurred. Possible options might include:

  • terminate: when true stops when first validation error is encountered. Otherwise errors for all fields are returned.
  • convert: automatically attempt to coerce values to the required type (i.e. string <-> number/boolean/date).

Examples

Rough outline of what this might look like in practice:

const schema = r.schema({
  code: r.string().min(3).max(9).upper(), // [A-Z\W]{3,9}
  date: r.date().min(r.time(2016, 6, 22, 'Z')).max(r.now()) // between 2016-06-22 and now
  value: r.number()
})

schema(r.expr({ code: 'ABC', date: r.now(), value: 5 }))
/* ->
{
  valid: true,
  data: {
    code: 'ABC',
    date: Date('2016-06-22'),
    value: 5
  }
}
*/

schema(r.expr({ code: 'AB', date: 'NOT_A_DATE', value: 5 }))
/* ->
{
  valid: false,
  data: {
    value: 5
  }
  errors: {
    code: { min: { required: 3, actual: 2 } },
    date: { type: { required: 'DATE', actual: 'STRING' } }
  }
}
*/

Activity

  1. added this to the subsequent milestone on Jun 24, 2016
  2. danielmewes commented on Jun 24, 2016

    @danielmewes
    Member

    Thanks for the great proposal @marshall007 .

    Linking related issues here: #5494 , #4210

  3. toddmedema commented on Jul 8, 2016

    @toddmedema

    Like where this is heading, just wanted to add that I'm a huge fan of how the Joi library does validation in case that provides any additional inspiration / insight

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions