import { Severity , Validatable , ValidationFailure } from "../shared" ; import { UnlessCondition , ValidationRule , WhenCondition , WhenDefinedCondition , WhenNotNullCondition } from "../validation" ; import { IsDefinedValidator , IsEmptyValidator , IsEqualValidator , IsInValidator , IsNotEmptyValidator , IsNotEqualValidator , IsNotInValidator , IsNotNullValidator , IsNullValidator , IsUndefinedValidator } from "../validators/common" ; import { CommonValidatorBuilder , ValidationOptionsBuilder } from "./" ; export class CommonValidatorBuilderImpl < T , TProperty > implements ValidationOptionsBuilder < T > , CommonValidatorBuilder < T , TProperty > { constructor ( protected validationRule ) { } withFailureCode ( errorCode ) { this . validationRule . setErrorCode ( errorCode ) ; return this ; } withFailureMessage ( errorMessage ) { this . validationRule . setErrorMessage ( errorMessage ) ; return this ; } withSeverity ( severity ) { this . validationRule . setSeverity ( severity ) ; return this ; } withPropertyName ( name ) { this . validationRule . setPropertyName ( name ) ; return this ; } whenDefined ( ) { this . validationRule . addCondition ( new WhenDefinedCondition ( this . validationRule . lambdaExpression ) ) ; return this ; } whenNotNull ( ) { this . validationRule . addCondition ( new WhenNotNullCondition ( this . validationRule . lambdaExpression ) ) ; return this ; } when ( expression ) { this . validationRule . addCondition ( new WhenCondition ( expression ) ) ; return this ; } unless ( expression ) { this . validationRule . addCondition ( new UnlessCondition ( expression ) ) ; return this ; } onFailure ( callback ) { this . validationRule . onFailure ( callback ) ; return this ; } fulfills ( validator ) ; fulfills ( validationExpression ) ; fulfills ( validatable ) { if ( typeof validatable === "object" ) { this . validationRule . addValidator ( { isValid : function ( input ) : boolean { return validatable . validate ( input ) . isValid ( ) ; } } ) ; } else { this . validationRule . addValidator ( { isValid : function ( input ) : boolean { return validatable ( input ) ; } } ) ; } return this ; } isDefined ( ) { this . validationRule . addValidator ( new IsDefinedValidator ( ) ) ; return this ; } isUndefined ( ) { this . validationRule . addValidator ( new IsUndefinedValidator ( ) ) ; return this ; } isNull ( ) { this . validationRule . addValidator ( new IsNullValidator ( ) ) ; return this ; } isNotNull ( ) { this . validationRule . addValidator ( new IsNotNullValidator ( ) ) ; return this ; } isEmpty ( ) { this . validationRule . addValidator ( new IsEmptyValidator ( ) ) ; return this ; } isNotEmpty ( ) { this . validationRule . addValidator ( new IsNotEmptyValidator ( ) ) ; return this ; } isEqualTo ( comparison ) { this . validationRule . addValidator ( new IsEqualValidator ( comparison ) ) ; return this ; } isNotEqualTo ( comparison ) { this . validationRule . addValidator ( new IsNotEqualValidator ( comparison ) ) ; return this ; } isIn ( iterable ) { this . validationRule . addValidator ( new IsInValidator ( iterable ) ) ; return this ; } isNotIn ( iterable ) { this . validationRule . addValidator ( new IsNotInValidator ( iterable ) ) ; return this ; } }