1.0.3 • Published 5 months ago

opticore-validator v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

OptiCore Validator

It a validator that allow to validate a data schema by many rule and customized message.

Install

npm i opticore-validator

ES6

import validator from 'opticore-validator';

Example

const schema = {
    name: [
        { rule: 'required', message: 'The name can not be empty' },
        { rule: 'minLength', args: [3] }
    ],
    password: [
        { rule: 'required', message: 'You must provide a value' },
        { rule: 'minLength', args: [8], message: 'A password must contain min 8 char.' },
        { rule: 'containsUppercase', message: 'You must to add upper case char in the password' },
        { rule: 'containsLowercase', message: 'you need to add lower case char' }
    ],
}
const dataPayload = {
    name: 'Jo',
    password: 'word1!',
}

result

{
  name: [ 'Field name failed validation rule minLength  with arguments 3.' ], // Th a default message
  password: [
    'A password must contain min 8 char.',
    'You must to add upper case char in the password'
  ]
}

Security sues

Validators

Here a lt of the validators currently available.

Rule ValidatorDescription
contains(value: any, seed: string)check if the value contains the seed.
equals(value: any, comparon: any)check if the value matches the comparon.
abaRouting(value: any)check if the value an ABA routing number for US bank account / cheque.
alpha(value: any)check if the value contains only letters (a-zA-Z).
alphanumeric(value: any)check if the value contains only letters and numbers (A-Za-z0-9).
ascii(value: any)check if the value contains ASCII chars only.
array(value: any)check if the value an array.
base32(value: any)check if the value base32 encoded.
base58(value: any)check if the value base58 encoded.
base64(value: any)check if the value base64 encoded.
bic(value: any)check if the value a BIC (Bank Identification Code) or SWIFT code.
boolean(value: any)check if the value a boolean. If loose set to false, the validator will strictly match 'true', 'false', '0', '1'. If loose set to true, the validator will also match 'yes', 'no', and will match a valid boolean value of any case. (e.g.: 'true', 'True', 'TRUE').
btcAddress(value: any)check if the value a valid BTC address.
byteLength(value: any, min: number, max?: number)check if the value's length (in UTF-8 bytes) falls in a range.you can definemin: 0, max: undefined`.
creditCard(value: any)check if the value a credit card number.
currency(value: any)check if the value a valid currency amount.
dataURI(value: any)check if the value a data uri format.
date(value: any)check if the value a valid date. e.g. 2002-07-15, new Date(). options an object which can contain the keys format, strictMode and/or delimiters.format a value and defaults to YYYY/MM/DD.strictMode a boolean and defaults to false. If strictMode set to true, the validator will reject strings different from format. delimiters an array of allowed date delimiters and defaults to ['/', '-'].
email(str , options)check if the value an email.options an object which defaults to { allow_dplay_name: false, require_dplay_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklted_chars: '', host_blacklt: [] }. If allow_dplay_name set to true, the validator will also match Dplay Name <email-address>. If require_dplay_name set to true, the validator will reject strings without the format Dplay Name <email-address>. If allow_utf8_local_part set to false, the validator will not allow any non-Englh UTF8 character in email address' local part. If require_tld set to false, email addresses without a TLD in their domain will also be matched. If ignore_max_length set to true, the validator will not check for the standard max length of an email. If allow_ip_domain set to true, the validator will allow IP addresses in the host part. If domain_specific_validation true, some additional validation will be enabled, e.g. dallowing certain syntactically valid email addresses that are rejected by Gmail. If blacklted_chars receives a value, then the validator will reject emails that include any of the characters in the value, in the name part. If host_blacklt set to an array of strings and the part of the email after the @ symbol matches one of the strings defined in it, the validation fails. If host_whitelt set to an array of strings and the part of the email after the @ symbol matches none of the strings defined in it, the validation fails.
notBlank(str , options)check if the value has a length of zero.options an object which defaults to { ignore_whitespace: false }.
ethereumAddress(str)check if the value an Ethereum address. Does not validate address checksums.
float(str , options)check if the value a float.options an object which can contain the keys min, max, gt, and/or lt to validate the float within boundaries (e.g. { min: 7.22, max: 9.55 }) it also has locale as an option.min and max are equivalent to 'greater or equal' and 'less or equal', respectively while gt and lt are their strict counterparts.locale determines the decimal separator and one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']. Locale lt validator.FloatLocales.
fqdn(str , options)check if the value a fully qualified domain name (e.g. domain.com).options an object which defaults to { require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, ignore_max_length: false }. If allow_wildcard set to true, the validator will allow domain starting with *. (e.g. *.example.com or *.shop.example.com).
freightContainerID(str)alias for O6346, check if the value a valid O 6346 shipping container identification.
fullWidth(str)check if the value contains any full-width chars.
halfWidth(str)check if the value contains any half-width chars.
hash(str, algorithm)check if the value a hash of type algorithm.Algorithm one of ['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192'].
hexadecimal(str)check if the value a hexadecimal number.
hexColor(str)check if the value a hexadecimal color.
hsl(str)check if the value an HSL (hue, saturation, lightness, optional alpha) color based on CSS Colors Level 4 specification.Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: hsl(200grad+.1%62%/1)).
IBAN(str, , options)check if the value an IBAN (International Bank Account Number).options an object which accepts two attributes: whitelt: where you can restrict IBAN codes you want to receive data from and blacklt: where you can remove some of the countries from the current lt. For both you can use an array with the following values ['AD','AE','AL','AT','AZ','BA','BE','BG','BH','BR','BY','CH','CR','CY','CZ','DE','DK','DO','EE','EG','ES','FI','FO','FR','GB','GE','GI','GL','GR','GT','HR','HU','IE','IL','IQ','IR','','IT','JO','KW','KZ','LB','LC','LI','LT','LU','LV','MC','MD','ME','MK','MR','MT','MU','MZ','NL','NO','PK','PL','PS','PT','QA','RO','RS','SA','SC','SE','SI','SK','SM','SV','TL','TN','TR','UA','VA','VG','XK'].