import * as ArrayLikeCheck from "./array-like"; import * as MixedCheck from "./generic/mixed"; export enum StringErrorCode { NOT_STRING = "NOT_STRING" } export function checkString (mixed : any) : undefined|StringErrorCode { if (typeof mixed === "string") { return undefined; } else { return StringErrorCode.NOT_STRING; } } export function checkStringPredicate (mixed : any, predicate : (str : string) => undefined|ErrorCodeT) : undefined|StringErrorCode|ErrorCodeT { const error = checkString(mixed); if (error !== undefined) { return error; } return predicate(mixed); } export type MinLengthErrorCodeType = StringErrorCode|ArrayLikeCheck.MinLengthErrorCodeType; export function checkMinLength (mixed : any, min : number) : undefined|MinLengthErrorCodeType { return checkStringPredicate(mixed, (str) => { return ArrayLikeCheck.checkMinLength(str, min); }); } export type MaxLengthErrorCodeType = StringErrorCode|ArrayLikeCheck.MaxLengthErrorCodeType; export function checkMaxLength (mixed : any, max : number) : undefined|MaxLengthErrorCodeType { return checkStringPredicate(mixed, (str) => { return ArrayLikeCheck.checkMaxLength(str, max); }); } export type LengthErrorCodeType = StringErrorCode|ArrayLikeCheck.LengthErrorCodeType; export function checkLength (mixed : any, args : { min? : number, max? : number }) : undefined|LengthErrorCodeType { return checkStringPredicate(mixed, (str) => { return ArrayLikeCheck.checkLength(str, args); }); } export type NonEmptyErrorCodeType = StringErrorCode|ArrayLikeCheck.NonEmptyErrorCodeType; export function checkNonEmpty (mixed : any) : undefined|NonEmptyErrorCodeType { return checkStringPredicate(mixed, (str) => { return ArrayLikeCheck.checkNonEmpty(str); }); } export type ContainsErrorCodeType = StringErrorCode|ArrayLikeCheck.ContainsErrorCodeType; export function checkContains (mixed : any, x : string) : undefined|ContainsErrorCodeType { return checkStringPredicate(mixed, (str) => { return ArrayLikeCheck.checkContains(str, x); }); } export type NoDuplicateErrorCodeType = StringErrorCode|ArrayLikeCheck.NoDuplicateErrorCodeType; export function checkNoDuplicate (mixed : any) : undefined|NoDuplicateErrorCodeType { return checkStringPredicate(mixed, (str) => { return ArrayLikeCheck.checkNoDuplicate(str); }); } export enum NotAllWhitespaceErrorCode { IS_ALL_WHITESPACE = "IS_ALL_WHITESPACE" } export function checkNotAllWhitespace (mixed : any) : undefined|StringErrorCode|NotAllWhitespaceErrorCode { return checkStringPredicate(mixed, (str) => { if (/$\s+^/.test(str)) { return NotAllWhitespaceErrorCode.IS_ALL_WHITESPACE; } else { return undefined; } }); } export function checkNonEmptyAndNotAllWhitespace (mixed : any) : undefined|StringErrorCode|NonEmptyErrorCodeType|NotAllWhitespaceErrorCode { const nonEmptyError = checkNonEmpty(mixed); if (nonEmptyError !== undefined) { return nonEmptyError; } const notAllWhitespaceError = checkNotAllWhitespace(mixed); if (notAllWhitespaceError !== undefined) { return notAllWhitespaceError; } return undefined; } export enum MatchErrorCode { DOES_NOT_MATCH = "DOES_NOT_MATCH" } export function checkMatch (mixed : any, regex : RegExp) : undefined|StringErrorCode|MatchErrorCode { return checkStringPredicate(mixed, (str) => { if (regex.test(str)) { return undefined; } else { return MatchErrorCode.DOES_NOT_MATCH; } }); } export enum EmailErrorCode { NOT_EMAIL = "NOT_EMAIL" } export function checkEmail (mixed : any) : undefined|StringErrorCode|EmailErrorCode { return checkStringPredicate(mixed, (str) => { if (/^.+@.+$/.test(str)) { return undefined; } else { return EmailErrorCode.NOT_EMAIL; } }); } export type NotOneOfErrorTypeCode = StringErrorCode|MixedCheck.NotOneOfErrorCode; export function checkNotOneOf (mixed : any, arr : T[]) : undefined|NotOneOfErrorTypeCode { return checkStringPredicate(mixed, (str) => { return MixedCheck.checkNotOneOf(str, arr); }); } export enum Base64ErrorCode { NOT_BASE64 = "NOT_BASE64", INVALID_BASE64 = "INVALID_BASE64" } export function checkBase64 (mixed : any) : undefined|StringErrorCode|Base64ErrorCode { return checkStringPredicate(mixed, (str) => { if (!/^[A-Za-z0-9\+\/]+\=*$/.test(str)) { return Base64ErrorCode.NOT_BASE64; } try { //If it fails to decode the base64 string, it will throw an error atob(str); } catch (_err) { return Base64ErrorCode.INVALID_BASE64; } return undefined; }); } export enum JsonErrorCode { INVALID_JSON = "INVALID_JSON" } export function checkJson (mixed : any) : undefined|StringErrorCode|JsonErrorCode { return checkStringPredicate(mixed, (str) => { try { //If it fails to parse the string, it will throw an error JSON.parse(str); } catch (_err) { return JsonErrorCode.INVALID_JSON; } return undefined; }); }