import { default as validateUuid } from 'uuid-validate' ; import { SchemaValidator , SchemaModel } from './schema' ; import { Path } from '@finnair/path' ; import { IgnoreValidator , ArrayNormalizer , AnyValidator , StringValidator , StringNormalizer , NotNullOrUndefinedValidator , IsNullOrUndefinedValidator , NotEmptyValidator , NotBlankValidator , ValueMapper , isNullOrUndefined , BooleanValidator , NumberValidator , NumberFormat , NumberNormalizer , DateValidator , ValidatorType , ValidatorFn , ValidatorFnWrapper , MappingFn , Validator , CheckValidator , maybeAllOfValidator , OptionalValidator , AssertTrue , IfValidator , Conditional , GroupOrName , WhenGroupValidator , WhenGroup , AssertTrueValidator , PatternValidator , PatternNormalizer , BooleanNormalizer , MinValidator , MaxValidator , ObjectModel , ObjectValidator , ObjectNormalizer , MapValidator , MapNormalizer , ArrayValidator , SizeValidator , AllOfValidator , AnyOfValidator , OneOfValidator , CompositionValidator , EnumValidator , HasValueValidator , JsonValidator , } from './validators' ; const ignoreValidator = new IgnoreValidator ( ) , anyValidator = new AnyValidator ( ) , stringValidator = new StringValidator ( ) , toStringValidator = new StringNormalizer ( ) , notNullValidator = new NotNullOrUndefinedValidator ( ) , nullOrUndefinedValidator = new IsNullOrUndefinedValidator ( ) , notEmptyValidator = new NotEmptyValidator ( ) , notBlankValidator = new NotBlankValidator ( ) , emptyToNullValidator = new ValueMapper ( val => ( isNullOrUndefined ( val ) || val === '' ? null : val ) ) , emptyToUndefinedValidator = new ValueMapper ( val => ( isNullOrUndefined ( val ) || val === '' ? undefined : val ) ) , undefinedToNullValidator = new ValueMapper ( val => ( val === undefined ? null : val ) ) , booleanValidator = new BooleanValidator ( ) , numberValidator = new NumberValidator ( NumberFormat . number ) , toNumberValidator = new NumberNormalizer ( NumberFormat . number ) , integerValidator = new NumberValidator ( NumberFormat . integer ) , toIntegerValidator = new NumberNormalizer ( NumberFormat . integer ) , dateValidator = new DateValidator ( ValidatorType . Date ) ; const V = { fn : ( fn , type ? ) => new ValidatorFnWrapper ( fn , type ) , map : ( fn , error ? ) => new ValueMapper ( fn , error ) , ignore : ( ) => ignoreValidator , any : ( ) => anyValidator , check : ( ... allOf ) => new CheckValidator ( maybeAllOfValidator ( allOf ) ) , optional : ( type , ... allOf ) => new OptionalValidator ( type , allOf ) , required : ( type , ... allOf ) => new NotNullOrUndefinedValidator ( ) . next ( type ) . next ( ... allOf ) , if : ( fn , ... allOf ) => new IfValidator ( [ new Conditional ( fn , allOf ) ] ) , whenGroup : ( group , ... allOf ) => new WhenGroupValidator ( [ new WhenGroup ( group , allOf ) ] ) , string : ( ) => stringValidator , toString : ( ) => toStringValidator , notNull : ( ) => notNullValidator , nullOrUndefined : ( ) => nullOrUndefinedValidator , notEmpty : ( ) => notEmptyValidator , notBlank : ( ) => notBlankValidator , emptyToNull : ( ) => emptyToNullValidator , emptyToUndefined : ( ) => emptyToUndefinedValidator , undefinedToNull : ( ) => undefinedToNullValidator , emptyTo : ( defaultValue ) => new ValueMapper ( val => ( isNullOrUndefined ( val ) || val === '' ? defaultValue : val ) ) , uuid : ( version ? ) => new AssertTrueValidator ( value => ! isNullOrUndefined ( value ) && validateUuid ( value , version ) , 'UUID' ) , pattern : ( pattern , flags ? ) => new PatternValidator ( pattern , flags ) , toPattern : ( pattern , flags ? ) => new PatternNormalizer ( pattern , flags ) , boolean : ( ) => booleanValidator , toBoolean : ( truePattern = /^true$/ , falsePattern = /^false$/ ) => new BooleanNormalizer ( truePattern , falsePattern ) , number : ( ) => numberValidator , toNumber : ( ) => toNumberValidator , integer : ( ) => integerValidator , toInteger : ( ) => toIntegerValidator , min : ( min , inclusive = true ) => new MinValidator ( min , inclusive ) , max : ( max , inclusive = true ) => new MaxValidator ( max , inclusive ) , object : ( model ) => new ObjectValidator ( model ) , toObject : ( property ) => new ObjectNormalizer ( property ) , schema : ( fn ) => new SchemaValidator ( fn ) , /** WARN: Objects as Map keys use identity hash/equals, i.e. === */ mapType : ( keys , values , jsonSafeMap = true ) => new MapValidator ( keys , values , jsonSafeMap ) , /** WARN: Objects as Map keys use identity hash/equals, i.e. === */ toMapType : ( keys , values ) => new MapNormalizer ( keys , values ) , nullTo : ( defaultValue ) => new ValueMapper ( value => ( isNullOrUndefined ( value ) ? defaultValue : value ) ) , array : ( ... items ) => new ArrayValidator ( maybeAllOfValidator ( items ) ) , toArray : ( ... items ) => new ArrayNormalizer ( maybeAllOfValidator ( items ) ) , size : ( min , max ) => new SizeValidator ( min , max ) , properties : ( keys , values ) => new ObjectValidator ( { additionalProperties : { keys , values } } ) , allOf : ( ... validators ) => new AllOfValidator ( validators ) , anyOf : ( ... validators ) => new AnyOfValidator ( validators ) , oneOf : ( ... validators ) => new OneOfValidator ( validators ) , compositionOf : ( ... validators ) => new CompositionValidator ( validators ) , date : ( ) => dateValidator , enum : ( enumType , name ) => new EnumValidator ( enumType , name ) , assertTrue : ( fn , type = 'AssertTrue' , path ? ) => new AssertTrueValidator ( fn , type , path ) , hasValue : ( expectedValue ) => new HasValueValidator ( expectedValue ) , json : ( ... validators ) => new JsonValidator ( validators ) , } ; export default V ;