0.1.0 • Published 2 years ago

evm-ts v0.1.0

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

EVM-Ts CircleCI

*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'
0.1.0

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago