0.1.4 • Published 4 years ago

validation-temp v0.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

useValidation

export interface  IUseValidation<T> {
 /** Check the field value in state and set field value to undefined if not set in the initial values.
  *  ref is used for additional control, like focus or input control
  * @param field  name of the field in model like  person.address.zip
  * @param ref : React.Ref
  */
 assignField : (field : string, ref : React.Ref<any>) => void,
 /** @param field - name of the field in model like  person.address.city
  *  @return current value for the field or undefined
  */
 addValidationRule : (field : string,  rule : IValidationFieldRules) => void
 getFieldValue : (field : string) => string | undefined,
 /** @param field  name of the field in model like  person.address.zip
  *  @return error for that field ( string or true) or false if error not exists for field
  */
 getFieldError : (field : string) => string | undefined,
 setFieldValue : (field : string, value : string) => void,
 setFieldError : (field : string, error : string) => void
 /** Set the global error for validation. This error is not like global model error, this is error that not belongs to any field
  * @param error - error like string or boolean false|true
  */
 setErrorGlobal : (error : string  |boolean) => void,
 /** function that is called when field has onBlur event, validation should be made and field set to dirty */
 onBlurField : (field : string, value ? : string) => void,
 /**
  * @return  true|false depends of is there error in validation or not. If error exists then return false and break
  *  if there is no error true is backed and function execute submitSuccess() function (entry param function )
  */
 submit : () => Promise<boolean>,
 /** Same like submit , make validation of model and return true\false, => validation passed/not */
 validate : () => Promise<boolean>,
 /** Reset all validation for model, all flags set to false, all field  set dirty: false */
 resetValidations : () => void,
/** State of the model */
 state : T
 /** Flag true|false that represent is there any error in validation in model, if one field or errorGlobal is set the this flag is true. */
 errorModel : boolean,
 /** value for error that is  set external ( setErrorGlobal ) and can be used to set error that can't be determine to belongs to any field */
 errorGlobal : boolean|string
}

export const useValidation = <T>( data: IUseValidationProps<T> ) : IUseValidation<T>;
Reset Validation

Reset the validation, clear all errors and make all fields not dirty

Validate

Async function that make form validation and return object This function will not execute additional actions like submit function

Remove Data Array

Use function to remove from list that has to be validated

 const validation = useValidation(...)
...
  const someHandler =  () => { 
      const { removeArrayData } = useValidation
      removeArrayData(fieldParentName as string, index as number)
  }  

Example: Client- Address

FieldDescription
getFieldValuefunction that returns current value for the field from state (or undefined) getField('email')
setFieldValueset the value to dedicated field in state setField('company.address.zip','12345')
setFieldErrorset the error for field - this error will be valid error for that field ( error is set and validation will not pass until field value is changed) setField('person.firstName','User already exists')
setErrorGlobalset the error for complete model but not for particular field. Used when you get error from backend that can't be dedicated to any field, param can be string to be shown as error or just true like error exists
errorGlobalValue of error global that is set external, can be boolean,undefined,string , undefined or false - error not exists
staterepresent object of form and it is regular state
onBlurFieldfunction that should be called after particular field got onBlur event, make field dirty.
errorModelboolean value that shows is there one error on model
resetValidationsReset Validation
submitSubmit Validation
validateValidate
removeArrayData Remove Array Data

Importing

Import useValidation hook and IValidationModelRules as main type interface. Second importing is to import all necessary validation functions to perform validation on form

import {  IValidationModelRules,  useValidation  } from 'validation/validator'
import {
          areTheSame,
          minLength,
          required,
          isEmail,
          maxLength
       }                         from 'validation/validations'
0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago