import validateJs = require ( 'validate.js' ) ; import { ValidatorError } from './validator-error' ; export class Validator { public static _validateJs ; private static _constructor = ( ( ) => { Validator . _validateJs = validateJs ; Validator . registerCustomValidators ( ) ; } ) ( ) ; private static registerCustomValidators ( ) { Validator . _validateJs . validators . notNull = Validator . notNullValidator ; Validator . _validateJs . validators . required = Validator . requiredValidator ; } static validate ( attributes , constraints , options ? ) { const validatorErrors = Validator . _validateJs ( attributes , constraints , { format : 'detailed' } ) ; if ( validatorErrors == null ) { return null ; } return validatorErrors . map ( error => { return new ValidatorError ( error . attribute , error . value , error . validator , error . error ) ; } ) ; } ; private constructor ( ) { } private static notNullValidator ( value , options , key , attributes ) { if ( value === null ) { const errorMessage = options . message || 'can not be null.' ; return errorMessage ; } return null ; } private static requiredValidator ( value , options , key , attributes ) { if ( value === undefined ) { const errorMessage = options . message || 'is required to be present.' ; return errorMessage ; } return null ; } }