import isEmpty from 'lodash/isEmpty' ; import isString from 'lodash/isString' ; import without from 'lodash/without' ; import keys from 'lodash/keys' ; import map from 'lodash/map' ; import get from 'lodash/get' ; import { BaseState } from '#typings/index' ; import { ResourceErrorType , BaseError } from '#typings/errors' ; import { AddressField , addressFields } from '#reducers/AddressReducer' ; import { AddressCreate } from '@commercelayer/sdk' ; const EMAIL_PATTERN = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$/ ; type FormField = HTMLInputElement | HTMLSelectElement ; export interface ValidateFormFields { < R extends string [ ] > ( fields , required , resourceType ) : { errors : BaseError [ ] ; values : BaseState ; } ; } export interface ValidateValue { < V extends string | boolean , N extends string , T extends string , B extends ResourceErrorType > ( val , name , type , resourceType ) : BaseError | Record < string , any > ; } export const validateValue = ( val , name , type , resourceType ) => { if ( ! val ) { return { field : name , code : 'VALIDATION_ERROR' , message : `${ name } - is required` , resourceType , } ; } if ( type === 'email' && isString ( val ) && ! val . match ( EMAIL_PATTERN ) ) { return { field : name , code : 'VALIDATION_ERROR' , message : `${ name } - is not valid` , resourceType , } ; } return { } ; } ; const validateFormFields = ( fields , required , resourceType ) => { const errors = [ ] ; let values = { metadata : { } } ; map ( fields , ( v ) => { const isTick = ! ! get ( v , 'checked' ) ; const val = isTick ? isTick : v . value === 'on' ? false : v . value ; const attrName = v . getAttribute ( 'name' ) ; if ( ( attrName && required . indexOf ( attrName ) !== - 1 ) || v . required ) { const error = validateValue ( val , v . name , v . type , resourceType ) ; if ( ! isEmpty ( error ) ) { errors . push ( error as BaseError ) ; } values = { ... values , [ `${ v . name }` ] : val } ; } if ( v . getAttribute ( 'name' ) ) { const isMetadata = ! ! v . getAttribute ( 'data-metadata' ) ; values = isMetadata ? { ... values , metadata : { ... values . metadata , [ `${ v . name }` ] : val } , } : { ... values , [ `${ v . name }` ] : val } ; } } ) ; return { errors , values } ; } ; export interface FieldsExist { ( address , schema ? ) : boolean ; } export const fieldsExist = ( address , schema = addressFields ) => { if ( ! address [ 'business' ] ) { const required = without ( schema , 'line_2' , 'company' ) ; const validAddress = keys ( address ) . filter ( ( k ) => required . includes ( k as any ) ) ; return required . length > validAddress . length ; } else { const required = without ( schema , 'first_name' , 'last_name' ) ; const validAddress = keys ( address ) . filter ( ( k ) => required . includes ( k as any ) ) ; return required . length > validAddress . length ; } } ; export default validateFormFields ;