Schema Providers
How to provide schemas from model constructors
This way of defining schemas is particularly useful when dealing with recursive models (see Recursive Schemas for an example).
To create a schema provider, the class or constructor function must provide a static getRevivalSchema
function.
When using a class, the getRevivalSchema
function must be static.
class Employee {
static getRevivalSchema(): RevivalSchema<Employee> {
return {
type: Employee,
properties: {
job: {
type: Job,
properties: {
props: JobProperties
}
},
friends: {
items: Employee
}
}
}
}
}
When using functions, the function must expose a getRevivalSchema
function.
function Employee() {
this.name = ''
}
Employee.getRevivalSchema = function() {
return {
type: Employee,
properties: {
job: {
type: Job,
properties: {
props: JobProperties
}
},
friends: {
items: Employee
}
}
}
}
This way, when reviving a model using revive(json, Employee)
the schema provided by the Employee
class will be used.
Last updated
Was this helpful?