Function: IndexSignature()
IndexSignature<
ClassConstructor>(schema):ClassDecorator<ClassConstructor>
Accept in a DTO the string keys without an @Expect schema, validated against a schema: they are kept instead of removed, and an invalid one fails the validation (on construction, on assignment, and in the class schema). Without it, the keys without schema are removed. Subclasses inherit it. FlexibleDTO is a DTO accepting any key.
Type Parameters
ClassConstructor
ClassConstructor extends typeof DTO
Parameters
schema
The schema of the value of each key without its own schema (DTO.Any() for any value).
Returns
ClassDecorator<ClassConstructor>
The class decorator.
Throws
From the DTO constructor, or from an assignment, when the value of a key without schema does not match the schema.
Example
typescript
import {DTO} from 'lakutata'
import {Expect, IndexSignature} from 'lakutata/decorator/dto'
//A device and its numeric readings, named freely
@IndexSignature(DTO.Number())
class ReadingsDTO extends DTO {
[sensor: string]: any
@Expect(DTO.String().required())
public device!: string
}
const readings: ReadingsDTO = new ReadingsDTO({device: 'boiler', temperature: 61.5, pressure: 1.8})
console.log(readings.temperature)