UNPKG

blow-validate

Version:
51 lines (50 loc) 1.31 kB
'use strict'; const rxjs_1 = require('rxjs'); class ValidationResult { constructor(errors) { this._errors = errors || []; } get isValid() { return !this._errors.length; } get errors() { return this._errors; } asObservable() { return rxjs_1.Observable.of(this); } merge(result) { this._errors = this._errors.concat(result.errors); return this; } addError(error) { this._errors.push(error); return this; } getInvalidProperties() { return this._errors.reduce((properties, error) => { if (properties.indexOf(error.property) === -1) { properties.push(error.property); } return properties; }, []); } getPropertyErrors(propertyName) { return this._errors.reduce((errors, error) => { if (error && error.property === propertyName) { errors.push(error); } return errors; }, []); } toJSON() { return { isValid: this.isValid, errors: this.errors.map(error => error.toJSON()) }; } static create(errors) { return new ValidationResult(errors); } } exports.ValidationResult = ValidationResult;