Schema
info
This concept is available for use only through the use of models without the use of decorators, see more.
Orionjs schema has two main purposes. First, it describes the structure of each Model, which will set automatically the GraphQL schema for that Model. Second, if you specify a Model in the setup of a Collection, it will validate the structure of a document before it's get inserted or updated into the MongoDB. You can also use the validate
Model function which will throw a ValidationError
if the object doesn't fit the given schema. It's important to note that validate
is async
.
Basic Usage
In Orionjs schemas are defined as a plain object with an specific structure.
Example
import {validate} from '@orion-js/schema'
const bookSchema = {
title: {
type: String,
label: 'Name'
},
author: {
type: String,
label: 'Author'
},
releaseDate: {
type: Date,
label: 'Release Date',
optional: true
}
}
// The following will pass
await validate(bookSchema, {title: 'The Book', author: 'The author'})
await validate(bookSchema, {title: 'The Book', author: 'The author', releaseDate: new Date()})
// The following will throw a ValidationError
await validate(bookSchema, {title: 'The Book'}) // The author is missing
await validate(bookSchema, {title: 'The Book', author: 'The author', releaseDate: 3}) // releaseDate should be type date
Schema rules
There are several options available, the only required is type
.
The options available are:
Type
The type
property has the following options:
String
or'string'
.Number
or'number'
.Date
or'date'
.Boolean
or'boolean'
.'ID'
allows you to save an id, which could be a string or an integer.'email'
checks if the email inserted has a valid format.'integer'
allows you to save integers.'blackbox'
allows you to save any object.- Custom, you can set as a type any Model.
All the types can be used as array, for example if you want an array of String
you should use
[String]
.
Label
The label
should be a String
. It's used by orionjs-react-autoform.
description
The description
should be a String
. It will be the description for GraphQL and it's used by orionjs-react-autoform
.
optional
By default, all keys are required. Set optional: true
to make a field optional.
private
This attribute cannot be queried from the API, not exposed in Graphql.
By default, all keys are not private. Set private: true
to make a field private.
min/max
- If
type
isNumber
or'integer'
, these rules define the minimum and maximum numeric value. - If
type
isString
, these rules define the minimum or maximum string length. - If
type
isDate
, these rules define the minimum or maximum date.
allowedValues
Should be an array of values.
- If
type
isNumber
,String
,Date
or'integer'
it will allow only values specified in this field. - If
type
is an array of any type, it will accept only values specified in this field.
defaultValue
Defines the default value for the field if nothing was given.
validate
This receives a function - which could be async
- with the following arguments:
fieldValue
: It's the value for the given field.options
: An object with the following keys:schema
: The full schema you are validating against.currentSchema
: The subpart of the schema you are validating.doc
: The full object you are validating.currentDoc
: The subpart of the object you are validating.
With this function you can make a custom validation of the value, it should return the error message if any and nothing if it has been successfully validated
Example, we will make a custom email validation without the use of the type email
const schema = {
email: {
type: String,
validate(fieldValue) {
// Regex to test if an email is valid
const regex =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (!regex.test(fieldValue)) return 'notAnEmail'
}
}
}
clean
This receives a function - which could be async
- with the following arguments:
fieldValue
: It's the value for the given field.options
: An object with the following keys:doc
: The full object you are validating.currentDoc
: The subpart of the object you are validating.
With this function you can make a custom clean of the value, it should return the new value for this field