into JavaScript custom objects so that we can do things like this:
constmyPet=M.fromJSON(Animal, petJson)myPet.speak() // => 'my name is Robbie!'
Here is how Animal would look like:
constM=require('modelico')classAnimalextendsM.createModel(m => ({ name:m.string()})) {speak() {constname=this.name()return (name ==='') ?`I don't have a name`:`My name is ${name}!` }}
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.
constM=require('modelico')constAjv=require('ajv')classAnimalextendsM.createAjvModel(newAjv(), m => ({ name:m.string({minLength:1})})) {speak () {constname=this.name()// the validation is only applied when reviving, not when creating new// instances directly (ie. new Animal({name: ''})) does not throwreturn (name ==='') ?`I don't have a name`:`My name is ${name}!` }}