1.1.4 • Published 2 years ago
@teamteanpm2024/tempora-consectetur-dolores v1.1.4
Doormen
Validate, sanitize and assert: the silver bullet of data!
Beta version.
/!\ This documentation is still a Work In Progress /!\
Basic validation
- sanitize
Arrayofstringthe sanitizer's name to apply before any type checking - optional
booleanthe data can benullorundefined, if so the data validate immediately - default (anything) the data can be
nullorundefined, if so it is overwritten by the default value and it validates immediately - type
stringthe name of the type checker - instanceOf
- min
- max
- length
- minLength
- maxLength
- match
- in
- notIn
- when
- properties
objectof schema, it iterates through each properties and checks that they all match their own schema - elements
Arraysame than properties but for arrays - only
booleanused in conjunction with properties or elements, it checks that no properties other than those listed are present - of
objectcontains one schema that will check each elements of an array or each properties
Type Checkers
Javascript primitive types:
- undefined: the data should be
undefined - null: the data should be
null - boolean: the data should be a
boolean, i.e.trueorfalse - number: the data should be a
number. NOTE thatInfinityandNaNare ok, so you may consider using real/float instead of number in almost all cases - string: the data should be a
string - object: the data should be an
Object - function: the data should be a
Function
Javascript/Node.js built-in types:
- array: the data should be an
Array - error: the data should be an instance of
Error - date: the data should be an instance of
Date - regexp: a regular expression
- buffer: the data should be a Node.js
Buffer
Common meta types:
- unset:
nullorundefined - real or float: a
numberthat is notNaNnor +/-Infinity - integer: a
numberthat is notNaNnor +/-Infinity, and that do not have decimal part - hex: a
stringrepresenting an hexadecimal number, having only 0-9, a-f and A-F characters - strictObject: an object that is not an array
- looseObject: object-like: object or function
Internet:
- ipv4: a
stringthat is a IPv4 address - ipv6: a
stringthat is a IPv6 address - ip: an IPv4 or IPv6 address
- hostname: a
stringthat is a valid hostname (domain + subdomain) - host: a hostname or an ip address
- url: a
stringthat is a valid URL (generic URL) including the scheme - weburl: a valid web/internet URL, a subset of the URL type
- email: a
stringthat is a valid email address
Misc:
- schema: an
objectwhich is a valid @teamteanpm2024/tempora-consectetur-dolores schema - mongoId: an
objectwhich is a MongoDB'sObjectIdOR astringthat is a correctObjectIdstring representation
Sanitizers
Common cast:
- toNumber: try to convert to a
number(mostly fromstring) - toReal or toFloat: try to convert to a float/real (not much difference with
toNumber) - toInteger: try to convert to an
integer, rounding the number if necessary - toString: try to convert to a
string - toBoolean: try to convert to a
booleanvery loosely, so it accepts strings like true/false, yes/no, on/off or numbers like 1/0, or fallback to truthy/falsy values. - toArray: try to convert to an
array, non-array are converted to an array with a single element - toDate: try to convert to a
Date(numberare timestamp,stringare parsed,objectare considered like a Date-object) - nullToUndefined: transform
nulltoundefined
String modifiers:
- trim: trim the string, removing whitespace at the beginning and the end
- toUpperCase: transform all characters into their upper-case counterparts
- toLowerCase: transform all characters into theier lower-case counterparts
- capitalize: transform to title case, each word starts with an upper-case
- titleCase: transform to title case, each word starts with an upper-case following lower-case characters (except ALL-CAPS words)
- latinize: transform letter to latin (remove accent, transform modified letters to their latin counterparts)
- dashToCamelCase: transform identifiers having word separated by dash/hyphen to camelCase
Enforcers:
- resize: resize a string if it does not match the required length/minLength/maxLength (truncate, or use leftPadding/rightPadding properties of the schema)
- removeExtraProperties: remove extra-properties from the object that are not in the schema (clone it, not in-place)
Misc:
- mongoId: convert a string to a MongoDB's
ObjectIdif possible