@valkyriestudios/validator
Version:
A lightweight configurable javascript validator
194 lines (193 loc) • 9.48 kB
TypeScript
import { isArray } from '@valkyriestudios/utils/array/is';
import { isNotEmptyArray } from '@valkyriestudios/utils/array/isNotEmpty';
import { isBoolean } from '@valkyriestudios/utils/boolean/is';
import { isDate } from '@valkyriestudios/utils/date/is';
import { isFormData } from '@valkyriestudios/utils/formdata/is';
import { isFunction } from '@valkyriestudios/utils/function/is';
import { isAsyncFunction } from '@valkyriestudios/utils/function/isAsync';
import { isNumber } from '@valkyriestudios/utils/number/is';
import { isInteger } from '@valkyriestudios/utils/number/isInteger';
import { isObject } from '@valkyriestudios/utils/object/is';
import { isNotEmptyObject } from '@valkyriestudios/utils/object/isNotEmpty';
import { isString } from '@valkyriestudios/utils/string/is';
import { isNotEmptyString } from '@valkyriestudios/utils/string/isNotEmpty';
import { equal } from '@valkyriestudios/utils/equal';
import { vAlphaNumSpaces } from './functions/vAlphaNumSpaces';
import { vAlphaNumSpacesMultiline } from './functions/vAlphaNumSpacesMultiline';
import { vBase64 } from './functions/vBase64';
import { vBetween } from './functions/vBetween';
import { vBetweenInclusive } from './functions/vBetweenInclusive';
import { vBlob } from './functions/vBlob';
import { vColorHex } from './functions/vColorHex';
import { vContinent } from './functions/vContinent';
import { vCountry } from './functions/vCountry';
import { vCountryAlpha3 } from './functions/vCountryAlpha3';
import { vDateString } from './functions/vDateString';
import { vDateISO, vDateDay } from './functions/vDateSpecs';
import { vEmail } from './functions/vEmail';
import { vFalse } from './functions/vFalse';
import { vFile } from './functions/vFile';
import { vGeoLatitude } from './functions/vGeoLatitude';
import { vGeoLongitude } from './functions/vGeoLongitude';
import { vGreaterThan } from './functions/vGreaterThan';
import { vGreaterThanOrEqual } from './functions/vGreaterThanOrEqual';
import { vGuid } from './functions/vGuid';
import { vIn } from './functions/vIn';
import { vLessThan } from './functions/vLessThan';
import { vLessThanOrEqual } from './functions/vLessThanOrEqual';
import { vLiteral } from './functions/vLiteral';
import { vNull } from './functions/vNull';
import { vPhone } from './functions/vPhone';
import { vTimeZone } from './functions/vTimeZone';
import { vSize } from './functions/vSize';
import { vSysMac } from './functions/vSysMac';
import { vSysIPv4 } from './functions/vSysIPv4';
import { vSysIPv6 } from './functions/vSysIPv6';
import { vSysIPv4_or_v6 } from './functions/vSysIPv4_or_v6';
import { vSysPort } from './functions/vSysPort';
import { vTrue } from './functions/vTrue';
import { vISBN, vISBN10, vISBN13 } from './functions/vISBN';
import { vSSN } from './functions/vSSN';
import { vEAN, vEAN8, vEAN13 } from './functions/vEAN';
import { vUlid } from './functions/vUlid';
import { vUndefined } from './functions/vUndefined';
import { vUuid, vUuidV1, vUuidV2, vUuidV3, vUuidV4, vUuidV5 } from './functions/vUuid';
import { vUrl } from './functions/vUrl';
import { vUrlNoQuery } from './functions/vUrlNoQuery';
import { vUrlImage, vUrlAudio, vUrlVideo, vUrlMedia } from './functions/vUrlExtensions';
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 vAlphaNumSpaces;
readonly alpha_num_spaces_multiline: typeof vAlphaNumSpacesMultiline;
readonly array: typeof isArray;
readonly array_ne: typeof isNotEmptyArray;
readonly base64: typeof vBase64;
readonly between: typeof vBetween;
readonly between_inc: typeof vBetweenInclusive;
readonly blob: typeof vBlob;
readonly boolean: typeof isBoolean;
readonly color_hex: typeof vColorHex;
readonly continent: typeof vContinent;
readonly country: typeof vCountry;
readonly country_alpha3: typeof vCountryAlpha3;
readonly date: typeof isDate;
readonly date_day: typeof vDateDay;
readonly date_iso: typeof vDateISO;
readonly date_string: typeof vDateString;
readonly ean: typeof vEAN;
readonly ean_8: typeof vEAN8;
readonly ean_13: typeof vEAN13;
readonly email: typeof vEmail;
readonly equal_to: typeof equal;
readonly false: typeof vFalse;
readonly file: typeof vFile;
readonly formdata: typeof isFormData;
readonly function: typeof isFunction;
readonly async_function: typeof isAsyncFunction;
readonly geo_latitude: typeof vGeoLatitude;
readonly geo_longitude: typeof vGeoLongitude;
readonly greater_than: typeof vGreaterThan;
readonly greater_than_or_equal: typeof vGreaterThanOrEqual;
readonly guid: typeof vGuid;
readonly in: typeof vIn;
readonly integer: typeof isInteger;
readonly isbn: typeof vISBN;
readonly isbn_10: typeof vISBN10;
readonly isbn_13: typeof vISBN13;
readonly less_than: typeof vLessThan;
readonly less_than_or_equal: typeof vLessThanOrEqual;
readonly literal: typeof vLiteral;
readonly max: typeof vLessThanOrEqual;
readonly min: typeof vGreaterThanOrEqual;
readonly null: typeof vNull;
readonly number: typeof isNumber;
readonly object: typeof isObject;
readonly object_ne: typeof isNotEmptyObject;
readonly phone: typeof vPhone;
readonly size: typeof vSize;
readonly ssn: typeof vSSN;
readonly string: typeof isString;
readonly string_ne: typeof isNotEmptyString;
readonly sys_mac: typeof vSysMac;
readonly sys_ipv4: typeof vSysIPv4;
readonly sys_ipv6: typeof vSysIPv6;
readonly sys_ipv4_or_v6: typeof vSysIPv4_or_v6;
readonly sys_port: typeof vSysPort;
readonly time_zone: typeof vTimeZone;
readonly true: typeof vTrue;
readonly ulid: typeof vUlid;
readonly url: typeof vUrl;
readonly url_noquery: typeof vUrlNoQuery;
readonly url_img: typeof vUrlImage;
readonly url_vid: typeof vUrlVideo;
readonly url_aud: typeof vUrlAudio;
readonly url_med: typeof vUrlMedia;
readonly uuid: typeof vUuid;
readonly uuid_v1: typeof vUuidV1;
readonly uuid_v2: typeof vUuidV2;
readonly uuid_v3: typeof vUuidV3;
readonly uuid_v4: typeof vUuidV4;
readonly uuid_v5: typeof vUuidV5;
readonly gt: typeof vGreaterThan;
readonly gte: typeof vGreaterThanOrEqual;
readonly lt: typeof vLessThan;
readonly lte: typeof vLessThanOrEqual;
readonly eq: typeof equal;
readonly '?': typeof 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 };