Created
June 19, 2023 10:30
-
-
Save maxgaurav/893f4ab17d7cb403631eaed1fb50cf1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FormControl, SyncValidator } from "universal-reactive-forms"; | |
import { ErrorKeys } from "@/components/ErrorKeys"; | |
/** | |
* Returns valid when the value is not null|undefined|empty string or when array is not empty | |
* @param value | |
* @constructor | |
*/ | |
export const Required: SyncValidator = (value) => { | |
if (value instanceof Array) { | |
return value.length === 0 ? { required: "true" } : null; | |
} | |
if (value === null || value === "" || value === undefined) { | |
return { required: "true" }; | |
} | |
if (typeof value === "string" && value.trim().length === 0) { | |
return { required: "true" }; | |
} | |
return null; | |
}; | |
/** | |
* Returns true if the value is valid number | |
* @param value | |
* @constructor | |
*/ | |
export const IsNumber: SyncValidator = (value) => { | |
return isNaN(parseFloat(value)) ? { number: "true" } : null; | |
}; | |
export const Min = | |
(minValue: number): SyncValidator => | |
(value) => { | |
const error = { | |
[ErrorKeys.Min]: `${minValue}`, | |
}; | |
if (IsNumber(value) !== null) { | |
return error; | |
} | |
return parseFloat(value) < minValue ? error : null; | |
}; | |
export const Max = | |
(minValue: number): SyncValidator => | |
(value) => { | |
const error = { | |
[ErrorKeys.Min]: `${minValue}`, | |
}; | |
if (IsNumber(value) !== null) { | |
return error; | |
} | |
return parseFloat(value) > minValue ? error : null; | |
}; | |
export const MinLength = | |
(minLengthValue: number): SyncValidator => | |
(value) => { | |
const error = { | |
[ErrorKeys.MinLength]: `${minLengthValue}`, | |
}; | |
if (typeof value !== "string") { | |
return error; | |
} | |
return value.trim().length < minLengthValue ? error : null; | |
}; | |
export const MaxLength = | |
(maxLengthValue: number): SyncValidator => | |
(value) => { | |
const error = { | |
[ErrorKeys.MaxLength]: `${maxLengthValue}`, | |
}; | |
if (typeof value !== "string") { | |
return error; | |
} | |
return value.length > maxLengthValue ? error : null; | |
}; | |
const EmailRegex = new RegExp( | |
/^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ | |
); | |
/** | |
* Validates email structure | |
* @param value | |
* @constructor | |
*/ | |
export const Email: SyncValidator = (value: any) => { | |
const defaultError = { | |
[ErrorKeys.Email]: "true", | |
}; | |
if (typeof value !== "string") { | |
return defaultError; | |
} | |
return EmailRegex.test(value) ? null : defaultError; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment