SchemaModel • Generate Model classes based on JSON Schema definition.
$ npm install @chialab/schema-model
# or
$ yarn add @chialab/schema-model
Use via cdn:
<script type="text/javascript" src="https://unpkg.com/@chialab/schema-model-js"></script>
The SchemaModel
object can be extended or use as factory to create Model classes dynamically.
import SchemaModel from '@chialab/schema-model';
class PersonModel extends SchemaModel {
static get schema() {
return {
type: 'object',
properties: {
id: {
type: 'number',
},
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
married: {
type: 'boolean',
},
},
required: ['id'],
};
}
}
let person = new PersonModel({
id: 1,
});
const PersonModel = SchemaModel.create({
type: 'object',
properties: {
id: {
type: 'number',
},
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
married: {
type: 'boolean',
},
},
required: ['id'],
});
let person = new PersonModel({
id: 1,
});
SchemaModel uses tv4 to validate model data on creation and update.
Use the .validate
method to retrieve the validation state of the model.
let person = new PersonModel();
let validation = person.validate();
console.log(validation.valid); // --> false
console.log(validation.error.message); // --> 'Missing required property: id'
When a set of data is pass to the constructor or to the .set
method, the model will try to validate them. If the validation fails, an exception is thrown and the new data will not be set.
try {
let person = new PersonModel({ firstName: 'Alan' });
} catch (err) {
console.log(err.message); // --> 'Missing required property: id'
}
try {
let person = new PersonModel({ id: 1, firstName: 'Alan' });
person.set({
lastName: 10,
});
} catch (err) {
console.log(err.message); // --> 'Invalid type: number (expected string)'
}
By the way, you can disabled the auto validation passing validate: false
as option for constructor/set.
let person = new PersonModel({ firstName: 'Alan' }, { validate: false });
let validation = person.validate();
console.log(validation.valid); // --> false
console.log(validation.error.message); // --> 'Missing required property: id'
In order to get an object representing model data, you can use the .toJSON
helper, which converts all model instances in javascript plain objects:
let person = new PersonModel({ id: 1, firstName: 'Alan' });
console.log(person); // --> PersonModel{ id: 1, firstName: 'Alan' }
console.log(person.toJSON()); // --> { id: 1, firstName: 'Alan' }
You can access a property of the model using the .get
method, or accessing directly to its reference:
let person = new PersonModel({ id: 1, firstName: 'Alan' });
console.log(person.get('id')); // --> 1
console.log(person.firstName); // --> 'Alan'
By the way, you should always use the .set
method to update the model:
let person = new PersonModel({ id: 1, firstName: 'Alan' });
// ok!
person.set({
lastName: 'Turing',
});
// no no no
person.lastName = 'Turing';
Using ES2015 classes (or Object.defineProperty
programmatically), you can define a custom getter and setter for a property:
import SchemaModel from '@chialab/schema-model';
class PersonModel extends SchemaModel {
static get schema() {
return {
type: 'object',
properties: {
id: {
type: 'number',
},
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
married: {
type: 'boolean',
},
},
required: ['id'],
};
}
set married(married) {
// passing the `internal: true` options you can set a private property.
this.set('married', !!married, { internal: true });
}
get married() {
// setup a default value for a property
return this.get('married', { internal: true }) || false;
}
}
let person = new PersonModel({
id: 1,
});
console.log(person.married); // --> false
person.set({ married: true });
console.log(person.married); // --> true
In order to build and test SchemaModel, the following requirements are needed:
Install the dependencies and run the build
script:
$ yarn install
$ yarn build
This will generate the UMD and ESM bundles in the dist
folder, as well as the declaration file.
Run the test
script:
$ yarn test
SchemaModel is released under the MIT license.