Semantic Validator
A functional semantic validator tool for validation of several types and rich content, inspired by prop-types from React.
Get started
Using NPM or Yarn
First, install it via npm.
npm i semantic-validator
Then, import it from dependencies and enjoy it.
; const validate = opshape id: is tags: op;
Using script tag
First, add a script tag with to the HTML page.
Then, just enjoy it.
var op = semanticValidatorop;var is = semanticValidatoris; var validate = opshape id: is tags: op;
Usage
The base unit of the semantic validator is a function which accepts the value as an argument for validation. We define it in TypeScript like below.
;
This validator tool has several methods in two types: validator operator (op) and validator creator (is).
For example, we can call a validator creator such as is.integer()
to create a validator which expects the number should be an integer. We can also call is.greaterThan(100)
to create a validator which expects the number is greater than 100.
If we want to match both conditions, we can use a validator operator like op.and(validator1, validator2)
to combine two validators.
; const isInt = is;; // => true;; // => false; const isGt100 = is;; // => true;; // => false; const isIntGt100 = op;; // => true;; // => false;; // => false;
You can also integrate other validation tools together. Just follow the validator pattern.
For example, there is a method isUUID(str, version)
for validating a string. We can make a validator like val => isUUID(val, 5)
and compose with other validator operators.
; const validate = opshape id: name: is;; // => true;; // => false;
Cheatsheet
Method | Description |
---|---|
Basic validator operators | |
> op.so(validator) |
Pass the validator. |
> op.not(validator) |
Not pass the validator. |
> op.and(...validators) |
Pass all validators. |
> op.or(...validators) |
Pass any validators. |
> op.every(validator) |
Pass the validator on all elements in an array. |
> op.some(validator) |
Pass the validator on any elements in an array. |
> op.shape({ ...validatorOfKeys }) |
Pass all validators for each properties of an object. |
> op.exact({ ...validatorOfKeys }) |
Pass all validators for each properties of an exact object. |
Converter validator operators | |
> op.convert(converter, validator) |
Pass the validator after converted. |
> op.toFloat(validator) |
Pass the validator after converted to a float. |
> op.toInteger(validator) |
Pass the validator after converted to an integer. |
> op.toLength(validator) |
Pass the validator after converted to the length. |
> op.toSplit(separator, validator) |
Pass the validator after splitted the string as an array. |
> op.toKeys(validator) |
Pass the validator after converted to keys of an object. |
> op.toValues(validator) |
Pass the validator after converted to values of an object. |
> op.toDate(validator) |
Pass the validator after converted to a date object. |
Basic validator creators | |
> is.same(value) |
Is the same as the base value? |
> is.oneOf(...values) |
Is the same as any base values? |
> is.defined() |
Is defined (not undefined)? |
> is.notDefined() |
Is undefined? |
> is.nul() |
Is null? |
> is.nil() |
Is undefined or null? |
> is.bool() |
Is a boolean? |
> is.number() |
Is a number? |
> is.string() |
Is a string? |
> is.object() |
Is a non null object? |
> is.func() |
Is a function? |
> is.symbol() |
Is a symbol? |
> is.instanceOf(constructor) |
Is an instance of the constructor (class)? |
> is.float() |
Is a valid (not NaN or Infinity ) float? |
> is.integer() |
Is a valid (not NaN or Infinity ) integer? |
> is.array() |
Is an array? |
> is.date() |
Is a valid (not Invalid Date ) date object? |
Math validator creators | |
> is.equalTo(num) |
Is the number equal to the base number? |
> is.greaterThan(num) |
Is the number greater than the base number? |
> is.atLeast(num) |
Is the number at least the base number? |
> is.lessThan(num) |
Is the number less than the base number? |
> is.atMost(num) |
Is the number at most the base number? |
> is.between(min, max) |
Is the number between the base numbers? |
> is.fromTo(min, max) |
Is the number from and to the base numbers? |
Text validator creators | |
> is.match(regexp) |
Is the string match the regular expression? |
> is.startsWith(wording) |
Is the string starts with the wording? |
> is.endsWith(wording) |
Is the string ends with the wording? |
> is.contains(wording) |
Is the string contains the wording? |
List validator creators | |
> is.includes(...includings) |
Is the array includes all includings? |
> is.excludes(...excludings) |
Is the array excludes all excludings? |
> is.restrictedBy(...allowedItems) |
Is the array only includes allowed items? |
> is.distinct() |
Is items of the array are all different? |
Date validator creators | |
> is.moment(date) |
Is the date object at the moment of the base date object? |
> is.laterThan(date) |
Is the date object later than the base date object? |
> is.atEarliest(date) |
Is the date object at earliest the base date object? |
> is.earlierThan(date) |
Is the date object earlier than the base date object? |
> is.atLatest(date) |
Is the date object at latest the base date object? |
Validator operator reference
Basic
op: so
Will be valid when the validator returns true. It is usually not necessary.
op
Example:
const validate = op;; // => true; // => false
op: not
Will be valid when the validator returns false.
op
Example:
const validate = op;; // => true; // => false
op: and
Will be valid when all validators return true.
op
Example:
const validate = op;; // => true; // => false; // => false
op: or
Will be valid when any validator returns true.
op
Example:
const validate = op;; // => true; // => true; // => false
op: every
Will be valid when validator returns true on all elements in the array.
op
Example:
const validate = op;; // => true; // => false
op: some
Will be valid when validator returns true on any element in the array.
op
Example:
const validate = op;; // => true; // => true; // => false
op: shape
Will be valid when value is an object and all validator returns true on each key.
opshape key1: validator1 key2: validator2 ...keysWithValidator
Example:
const validate = opshape id: is name: is;; // => true; // => true; // => false; // => false
op: exact
Will be valid when value is an object with specific keys and all validator returns true on each key.
op
Example:
const validate = op;; // => true; // => false; // => false; // => false
Converter
op: convert
Will be valid when successfully converts the value and the validation is success.
op
Example:
const validate = op;; // => true; // => false
op: to float
Will be valid when successfully converts to a float and the validation is success.
op
Example:
const validate = op;; // => true; // => false; // => false
op: to integer
Will be valid when successfully converts to an integer and the validation is success.
op
Example:
const validate = op;; // => true; // => false; // => false; // => false
op: to length
Will be valid when successfully get the length of an array or string and the validation is success.
op
Example:
const validate = op;; // => true; // => true; // => false; // => false; // => false
op: to split
Will be valid when successfully split to the array and the validation is success.
op
Example:
const validate = op;; // => true; // => false; // => false
op: to keys
Will be valid when successfully get keys of an object and the validation is success.
op
Example:
const validate = op;; // => true; // => false
op: to values
Will be valid when successfully get values of an object and the validation is success.
op
Example:
const validate = op;; // => true; // => false
Validator creator reference
Basic
is: same
Will be valid when base value and compare value are the same by using the SameValueZero algorithm.
is
Example:
const validate = is;; // => true
is: one of
Will be valid when one of base values and compare value are the same by using the SameValueZero algorithm.
is
Example:
const validate = is;; // => true
is: defined
Will be valid when the value is defined (not undefined).
is
Example:
const validate = is;; // => true; // => false
is: not defined
Will be valid when the value is not defined (undefined).
is
Example:
const validate = is;; // => true; // => false
is: nul
Will be valid when the value is null.
is
Example:
const validate = is;; // => true; // => false
is: nil
Will be valid when the value is undefined or null.
is
Example:
const validate = is;; // => true; // => true; // => false
is: bool
Will be valid when the value is a boolean.
is
Example:
const validate = is;; // => true; // => false
is: number
Will be valid when the value is a number.
is
Example:
const validate = is;; // => true; // => false
is: string
Will be valid when the value is a string.
is
Example:
const validate = is;; // => true; // => false
is: object
Will be valid when the value is a non null object.
isobject
Example:
const validate = isobject;; // => true; // => true; // => false; // => false
is: func
Will be valid when the value is a function.
is
Example:
const validate = is;; // => true; // => false
is: symbol
Will be valid when the value is a symbol.
is
Example:
const validate = is;; // => true; // => false
is: instance of
Will be valid when the value is the instance of a constructor.
is
Example:
const validate = is;; // => true; // => false
is: float
Will be valid when the value is a float and not NaN or infinity.
is
Example:
const validate = is;; // => true; // => true; // => false
is: integer
Will be valid when the value is a integer and not NaN or infinity.
is
Example:
const validate = is;; // => true; // => false
is: array
Will be valid when the value is an array object.
is
Example:
const validate = is;; // => true
is: date
Will be valid when the value is a valid date object.
is
Example:
const validate = is;; // => true; // => false; // => false
Math
is: equal to
Will be valid when the value is equal to the number.
is
Example:
const validate = is;; // true; // false
is: greater than
Will be valid when the value is greater than the number.
is
Example:
const validate = is;; // => true; // => false
is: at least
Will be valid when the value is at least the number.
is
Example:
const validate = is;; // => true; // => false
is: less than
Will be valid when the value is less than the number.
is
Example:
const validate = is;; // => true; // => false
is: at most
Will be valid when the value is at most the number.
is
Example:
const validate = is;; // => true; // => false
is: between
Will be valid when the value is between the numbers.
is
Example:
const validate = is;; // => true; // => false; // => false
is: from to
Will be valid when the value is from and to the numbers.
is
Example:
const validate = is;; // => true; // => true; // => false; // => false
Text
is: match
Will be valid when the value matches the regular expression.
is
Example:
const validate = is;; // => true; // => false
is: starts with
Will be valid when the value starts with the wording.
is
Example:
const validate = is;; // => true; // => false
is: ends with
Will be valid when the value ends with the wording.
is
Example:
const validate = is;; // => true; // => false
is: contains
Will be valid when the value contains the wording.
is
Example:
const validate = is;; // => true; // => false
List
is: includes
Will be valid when the array value includes all includings.
is
Example:
const validate = is;; // => true; // => false
is: excludes
Will be valid when the array value excludes all excludings.
is
Example:
const validate = is;; // => true; // => false
is: restricted by
Will be valid when the array value is restricted by allowed items.
is
Example:
const validate = is;; // => true; // => false
is: distinct
Will be valid when items of array value are all different.
is
Example:
const validate = is;; // => true; // => false