Create objects and validate the values, so you know all values are ok. You don't have to create code to validate all fields of an object, just write a schema. Also get more usefull methods to work with objects.
npm install @trojs/objects
or
yarn add @trojs/objects
npm run test
or
yarn test
For validate the objects, it use the package @trojs/validation (https://www.npmjs.com/package/@trojs/validator)
It will throw an error if the object isnt valid.
For all posibilities, check the validation package readme.
Example usage:
import{Obj}from'@trojs/objects'constaddressSchema={street: String,number: Number,postalCode: String,city: String,country: String,};constAddress=Obj({schema: addressSchema})constmyAddress=Address.create({street: 'Abc',number: 42,postalCode: '1234AB',city: 'Example',country: 'The Netherlands'})console.log(myAddress){street: 'Abc',number: 42,postalCode: '1234AB',city: 'Example',country: 'The Netherlands'}You can also define sub schemas:
import{Obj}from'@trojs/objects'constfilterSchema={key: String,type: String}constoptionSchema={value: String,'count?': Number}constselectFilterSchema={
...filterSchema,options: optionSchema}constSelectFilter=Obj({schema: selectFilterSchema})constmultiselect=SelectFilter.create({key: 'status',type: 'multiselect',options: [{value: 'open',count: 42},{value: 'closed'}]})console.log(multiselect){street: 'status',number: 'multiselect',options: [{value: 'open',count: 42},{value: 'closed'}]}Catching validation errors:
try{constmultiselect=SelectFilter.create({key: 'status',options: [{value: 'open',count: 42},{value: 'closed'}]})}catch(error){console.log(error.message)}'The field type should be a String'You can also map the values:
consttestSchema={a: Number,b: Number,};constTest=Obj({schema: testSchema});constinput={a: 1,b: 2,};consttest=Test.create(input)constmappedResults=test.map((x)=>x*2)// Or you can do:constdouble=(x)=>x*2constmappedResults2=test.map(double)console.log(mappedResults){a: 2,b: 4,};You can also filter the values:
consttestSchema={a: Number,b: Number,};constTest=Obj({schema: testSchema});constinput={a: 1,b: 2,};consttest=Test.create(input)constfilteredResults=test.filter((x)=>x===1)// Or you can do:constonlyOne=(x)=>x===1constfilteredResults2=test.filter(onlyOne)console.log(filteredResults){a: 1,};You can also use every:
consttestSchema={a: Number,b: Number,};constTest=Obj({schema: testSchema});constinput={a: 1,b: 2,};consttest=Test.create(input)consteverythingIsBelowThree=test.every((x)=>x<3)// Or you can do:constisBelowThree=(x)=>x<3consteverythingIsBelowThree2=test.every(isBelowThree)console.log(everythingIsBelowThree)trueYou can also use some:
consttestSchema={a: Number,b: Number,};constTest=Obj({schema: testSchema});constinput={a: 1,b: 2,};consttest=Test.create(input)constsomeAreTwo=test.some((x)=>x===2)// Or you can do:constisTwo=(x)=>x===2constsomeAreTwo2=test.some(isTwo)console.log(someAreTwo)trueIf you have some data, and you have to transform the data to the schema, you can use the parse method.
constsubSchema={a: Number,b: Boolean,'c?': String,};consttestSchema={a: Number,b: Boolean,'c?': String,
subSchema,};constTest=Obj({schema: testSchema});constinput={a: '42',b: 'true',c: 42,subSchema: {a: '42',b: 'true',c: 42,},};Test.parse(input){a: 42,b: true,c: '42',subSchema: {a: 42,b: true,c: '42',},};Example usage without a schema: ...
constflatter=Obj().create({a: 1,b: 2,c: [3,4],d: {e: 5,f: 6},g: {h: {i: 7}}})You can get the flat object by:
flatter.flat{a: 1,b: 2,"c.0": 3,"c.1": 4,"d.e": 5,"d.f": 6,"g.h.i": 7}You can get the object entries by:
flatter.entries()[["a",1],["b",2],["c.0",3],["c.1",4],["d.e",5],["d.f",6],["g.h.i",7]]You can get the object keys by:
flatter.keys()["a","b","c.0","c.1","d.e","d.f","g.h.i"]You can get the object values by:
flatter.values()[1,2,3,4,5,6,7]You can get the object length by:
flatter.length7You can get by key by:
flatter.getByKey("g.h.i")7Or set a fallback value by:
flatter.getByKey("x.y.z",42)42flatter.getByKey("d"){e: 5,f: 6}Check if the object has a key:
flatter.has("g.h.i")trueCheck if the object has key includes a value:
flatter.includes("g.h")trueYou can get only some fields from the object:
flatter.getKeys(['a','c','d.e','g.h']){a: 1,c: [3,4],'d.e': 5,'g.h': {i: 7},}And you can also get some fields from the object in the flat format:
flatter.getFlatKeys(['a','c','d.e','g.h']){a: 1,'c.0': 3,'c.1': 4,'d.e': 5,'g.h.i': 7,}And you can also use the map method:
flatter.flatMap((x)=>x*2){a: 2,b: 4,'c.0': 6,'c.1': 8,'d.e': 10,'d.f': 12,'g.h.i': 14,};And you can also use the flatFilter method:
constflatter=Obj().create({a: 1,b: 2,c: [1,2],d: {e: 1,f: 2},g: {h: {i: 1}}})flatter.flatFilter((x)=>x===1){a: 1,'c.0': 1,'d.e': 1,'g.h.i': 1,};And you can also use the flatEvery method:
constflatter=Obj().create({a: 1,b: 2,c: [1,2],d: {e: 1,f: 2},g: {h: {i: 1}}})flatter.flatEvery((x)=>x<3)trueAnd you can also use the flatSome method:
constflatter=Obj().create({a: 1,b: 2,c: [1,2],d: {e: 1,f: 2},g: {h: {i: 1}}})flatter.flatSome((x)=>x===2)trueFor arrays you can also use createAll and parseAll.
It works the same as create and parse but it can do it on every item in an array.