UNPKG

@valkyriestudios/validator

Version:

A lightweight configurable javascript validator

148 lines (147 loc) 7.04 kB
import { isArray, isNeArray } from '@valkyriestudios/utils/array'; import { isBoolean } from '@valkyriestudios/utils/boolean'; import { isDate } from '@valkyriestudios/utils/date'; import { isFormData } from '@valkyriestudios/utils/formdata'; import { isFn, isAsyncFn } from '@valkyriestudios/utils/function'; import { isNum, isInt } from '@valkyriestudios/utils/number'; import { isObject, isNeObject } from '@valkyriestudios/utils/object'; import { isString, isNeString } from '@valkyriestudios/utils/string'; import { equal } from '@valkyriestudios/utils/equal'; import * as VR from './functions/index'; import { type DeepMutable, type GenericObject, type RulesRaw, type RuleExtension, type RuleFn, type ValidationResult, type InferredSchema, type MergeExtensions, type MappedExtensions, type TV } from './internalTypes'; declare const RULE_STORE: { readonly alpha_num_spaces: typeof VR.vAlphaNumSpaces; readonly alpha_num_spaces_multiline: typeof VR.vAlphaNumSpacesMultiline; readonly array: typeof isArray; readonly array_ne: typeof isNeArray; readonly base64: typeof VR.vBase64; readonly between: typeof VR.vBetween; readonly between_inc: typeof VR.vBetweenInclusive; readonly blob: typeof VR.vBlob; readonly boolean: typeof isBoolean; readonly color_hex: typeof VR.vColorHex; readonly continent: typeof VR.vContinent; readonly country: typeof VR.vCountry; readonly country_alpha3: typeof VR.vCountryAlpha3; readonly cron: typeof VR.vCron; readonly date: typeof isDate; readonly date_day: typeof VR.vDateDay; readonly date_iso: typeof VR.vDateISO; readonly date_string: typeof VR.vDateString; readonly ean: typeof VR.vEAN; readonly ean_8: typeof VR.vEAN8; readonly ean_13: typeof VR.vEAN13; readonly email: typeof VR.vEmail; readonly equal_to: typeof equal; readonly false: typeof VR.vFalse; readonly file: typeof VR.vFile; readonly formdata: typeof isFormData; readonly function: typeof isFn; readonly async_function: typeof isAsyncFn; readonly geo_latitude: typeof VR.vGeoLatitude; readonly geo_longitude: typeof VR.vGeoLongitude; readonly greater_than: typeof VR.vGreaterThan; readonly greater_than_or_equal: typeof VR.vGreaterThanOrEqual; readonly guid: typeof VR.vGuid; readonly in: typeof VR.vIn; readonly integer: typeof isInt; readonly isbn: typeof VR.vISBN; readonly isbn_10: typeof VR.vISBN10; readonly isbn_13: typeof VR.vISBN13; readonly less_than: typeof VR.vLessThan; readonly less_than_or_equal: typeof VR.vLessThanOrEqual; readonly literal: typeof VR.vLiteral; readonly max: typeof VR.vLessThanOrEqual; readonly min: typeof VR.vGreaterThanOrEqual; readonly null: typeof VR.vNull; readonly number: typeof isNum; readonly object: typeof isObject; readonly object_ne: typeof isNeObject; readonly phone: typeof VR.vPhone; readonly size: typeof VR.vSize; readonly ssn: typeof VR.vSSN; readonly string: typeof isString; readonly string_ne: typeof isNeString; readonly sys_mac: typeof VR.vSysMac; readonly sys_ipv4: typeof VR.vSysIPv4; readonly sys_ipv6: typeof VR.vSysIPv6; readonly sys_ipv4_or_v6: typeof VR.vSysIPv4_or_v6; readonly sys_port: typeof VR.vSysPort; readonly time_zone: typeof VR.vTimeZone; readonly true: typeof VR.vTrue; readonly ulid: typeof VR.vUlid; readonly url: typeof VR.vUrl; readonly url_noquery: typeof VR.vUrlNoQuery; readonly url_img: typeof VR.vUrlImage; readonly url_vid: typeof VR.vUrlVideo; readonly url_aud: typeof VR.vUrlAudio; readonly url_med: typeof VR.vUrlMedia; readonly uuid: typeof VR.vUuid; readonly uuid_v1: typeof VR.vUuidV1; readonly uuid_v2: typeof VR.vUuidV2; readonly uuid_v3: typeof VR.vUuidV3; readonly uuid_v4: typeof VR.vUuidV4; readonly uuid_v5: typeof VR.vUuidV5; readonly gt: typeof VR.vGreaterThan; readonly gte: typeof VR.vGreaterThanOrEqual; readonly lt: typeof VR.vLessThan; readonly lte: typeof VR.vLessThanOrEqual; readonly eq: typeof equal; readonly '?': typeof VR.vUndefined; }; type CustomRuleDictionary = Record<string, RuleFn>; type RuleDictionary = typeof RULE_STORE & CustomRuleDictionary; type CombinedRules<Extensions = {}> = typeof RULE_STORE & Extensions; export interface IValidator<Extensions extends Record<string, unknown> = {}> { new <T extends GenericObject, TypedValidator = TV<T>>(schema: TypedValidator): Validator<T, Extensions>; readonly rules: Readonly<CombinedRules<Extensions>>; extend<NewExtensions extends Record<string, RuleExtension>>(extensions: NewExtensions): IValidator<MergeExtensions<Extensions, MappedExtensions<NewExtensions, typeof RULE_STORE>>>; create<const TSchema extends RulesRaw>(schema: TSchema): Validator<InferredSchema<TSchema, CombinedRules<Extensions>>>; } declare class Validator<T extends GenericObject, Extensions = {}, TypedValidator = TV<T>> { #private; constructor(schema: TypedValidator); /** * Getter for configured schema. * @note Using this with typeof (eg: typeof myValidator.schema) returns the type of the store */ get schema(): DeepMutable<T>; /** * Checks if the provided data is valid against the validator's rules * * @param {GenericObject|FormData} raw - Raw object or FormData instance to check */ check(raw: unknown): raw is typeof this['schema']; /** * Checks if a FormData instance is valid against the validator and returns its parsed * content as an object if it is * * @param {FormData} raw - FormData instance to check */ checkForm(raw: FormData): false | this["schema"]; /** * Fully validates the provided data against the validator's rules * * @param {GenericObject|FormData} raw - Raw object or FormData instance to check */ validate<K extends GenericObject | FormData>(raw: K): ValidationResult; /** * Getter for the rules object on the validator */ static get rules(): Readonly<RuleDictionary>; /** * Extend the Validator * * Example: * Validator.extend({ * is_fruit: ['apple', 'pear', 'orange'], * ... * }); * * @param {Record<string, RuleExtension>} obj - KV Map of rule extensions */ static extend<const NewExtensions extends Record<string, RuleExtension>>(obj: NewExtensions): IValidator<MergeExtensions<{}, MappedExtensions<NewExtensions, typeof Validator['rules']>>>; static create<const TSchema extends RulesRaw | readonly RulesRaw[]>(schema: TSchema): Validator<TSchema extends readonly RulesRaw[] ? InferredSchema<TSchema[number], typeof Validator['rules']> : InferredSchema<TSchema, typeof Validator['rules']>>; static create<const TValidators extends readonly Validator<any>[]>(validators: TValidators): Validator<TValidators[number] extends Validator<infer U, any, any> ? U : never>; } export { Validator, Validator as default };