UNPKG

@chatterton/angular2-schema-form

Version:

Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)

29 lines (21 loc) 625 B
let ZSchema = require('z-schema'); export abstract class SchemaValidatorFactory { abstract createValidatorFn(schema): (value: any) => any; } export class ZSchemaValidatorFactory extends SchemaValidatorFactory { private zschema; constructor() { super(); this.zschema = new ZSchema({}); } createValidatorFn(schema: any) { return (value): { [key: string]: boolean } => { if (schema.type === 'number' || schema.type === 'integer') { value = +value; } this.zschema.validate(value, schema); let err = this.zschema.getLastErrors(); return err || null; }; } }