With a bit more effort, we can have a detailed JSON schema, with the benefits that they bring, eg. integration with JSON Shema Faker to generate random data.
const M = require('modelico')
const Ajv = require('ajv')
class Animal extends M.createAjvModel(new Ajv(), m => ({
name: m.string({minLength: 1})
})) {
speak () {
const name = this.name()
// the validation is only applied when reviving, not when creating new
// instances directly (ie. new Animal({name: ''})) does not throw
return (name === '') ? `I don't have a name` : `My name is ${name}!`
}
}