11import { ValidationResult } from "../../model/ValidationResult" ;
2- import { InputPropertiesTypes , QuestionModel , SliderProperties } from "../../model" ;
2+ import { InputPropertiesTypes , QuestionModel } from "../../model" ;
33import { registerQuickFormService } from "../QuickFormServices" ;
4+ import { QuickformState } from "../../state" ;
45
56const validateText = ( output : any ) : ValidationResult => {
67 const text = typeof output === 'string' ? output . trim ( ) : '' ;
@@ -36,8 +37,8 @@ const validateEmail = (output: any): ValidationResult => {
3637} ;
3738
3839const validatePhone = async ( output : any ) : Promise < ValidationResult > => {
39- // Wait for 4 seconds
40- await new Promise ( resolve => setTimeout ( resolve , 4000 ) ) ;
40+ // Wait for 2 seconds to demo
41+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
4142
4243 const phoneRegex = / ^ [ 0 - 9 ] { 8 , } $ / ;
4344 const valid = typeof output === 'string' && phoneRegex . test ( output ) ;
@@ -49,29 +50,18 @@ const validatePhone = async (output: any): Promise<ValidationResult> => {
4950 } ;
5051} ;
5152
52-
53- const validateSlider = ( output : any , properties : SliderProperties ) : ValidationResult => {
54- const valid = typeof output === 'number' && output >= properties . min && output <= properties . max ;
55- return {
56- isValid : valid ,
57- message : valid ? "" : `Value must be a number between ${ properties . min } and ${ properties . max } .` ,
58- validatedOutput : output ,
59- } ;
60- } ;
61-
6253type ValidatorMap = {
63- [ inputType : string ] : ( output : any , properties ?: any ) => Promise < ValidationResult > ;
54+ [ inputType : string ] : ValidatorFunction < any , any , QuestionModel < any > , QuickformState > ;
6455} ;
6556
6657const validatorMap : ValidatorMap = {
6758 email : ( output : any ) => Promise . resolve ( validateEmail ( output ) ) ,
6859 phone : ( output : any ) => Promise . resolve ( validatePhone ( output ) ) ,
69- slider : ( output : any , properties : SliderProperties ) => Promise . resolve ( validateSlider ( output , properties ) ) ,
7060 text : ( output : any ) => Promise . resolve ( validateText ( output ) ) ,
7161 multilinetext : ( output : any ) => Promise . resolve ( validateMultilineText ( output ) )
7262} ;
7363
74- const validateQuestionOutput = async < TProps extends InputPropertiesTypes > ( questionModel : QuestionModel < TProps > ) : Promise < ValidationResult > => {
64+ const validateQuestionOutput = async < TProps extends InputPropertiesTypes > ( questionModel : QuestionModel < TProps > , state : QuickformState ) : Promise < ValidationResult > => {
7565 const validator = validatorMap [ questionModel . inputType ] ;
7666 if ( ! validator ) {
7767 // This is to support if no validation is created for inputtype.. defaults to validated..
@@ -82,14 +72,15 @@ const validateQuestionOutput = async <TProps extends InputPropertiesTypes>(quest
8272 isValidating : false ,
8373 timestamp : new Date ( ) . getTime ( )
8474 } ) ;
85- // return Promise.resolve({ isValid: false, message: `No validator available for inputType: ${questionModel.inputType}`, validatedOutput: questionModel.output });
8675 }
8776
88- return await validator ( questionModel . output , questionModel . inputProperties ) ;
77+ return await validator ( questionModel . output , questionModel . inputProperties , questionModel , state ) ;
8978} ;
9079
91- export const registerInputTypeValidator = ( key : string , validator : ( output : any , properties ?: any ) => Promise < ValidationResult > ) => {
92- validatorMap [ key ] = validator ;
80+ export type ValidatorFunction < TAnswer , TInputProps , TQuestionModel extends QuestionModel < TInputProps > , TQuickFormState extends QuickformState > = ( output : TAnswer , properties : TInputProps , questionModel : TQuestionModel , state : TQuickFormState ) => Promise < ValidationResult > ;
81+
82+ export const registerInputTypeValidator = < TAnswer , TInputProps , TQuestionModel extends QuestionModel < TInputProps > , TQuickFormState extends QuickformState > ( key : string , validator : ValidatorFunction < TAnswer , TInputProps , TQuestionModel , TQuickFormState > ) => {
83+ validatorMap [ key ] = validator as ValidatorFunction < any , any , QuestionModel < any > , QuickformState > ;
9384} ;
9485
9586registerQuickFormService ( "inputValidator" , validateQuestionOutput ) ;
0 commit comments