evm-ts
v0.1.0
Published
Entity validation and mapping for typescript.
Downloads
1
Readme
EVM-Ts
Entity validation and mapping for typescript
*=> Validates json data and maps it into actual and valid typescript classes.
- jokingly small (no dependencies) and easy as 🍰 to use
- useful errors when validations fail
- ✅ full test coverage
- validates all json types (including other objects and lists)
- required, optional and default value properties
- use real classes with methods instead of interfaces
Installation
TODO
Usage
1. define your EvmEntities and just add the EvmSchema
class SampleEntity implements EvmEntity {
id!: number
text!: string
optional?: string
doubleId(): number { return id * 2 }
_evmSchema = {
evmId: 'SampleEntity',
properties: [
{ id: 'id', type: EvmPropType.Number },
{ id: 'text', type: EvmPropType.String },
{ id: 'optional', type: EvmPropType.String, required: false },
]
}
}
2. map the data from your backend
const jsonData = { firstProperty: 3, anotherProperty: 'test' }
const sampleInstance = EvmMapper.getEntity(jsonData, SampleEntity)
sampleInstance.id // => 3
sampleInstance.doubleId() // => 6
sampleInstance.optional // => undefined
3. get notified on failed validations
const jsonData = { firstProperty: 3, listOfOtherEntity: [ { anotherList: [ true, 'false' ] } ] }
EvmMapper.getEntity(jsonData, SampleEntity)
// => EvmEntityError: 'SampleEntity.listOfOtherEntity[0].anotherList[1]' expected 'boolean' but was 'string'