🦩 Funval
A minimalist library for data validation using functions interfaces.
WARNING: This package is deprecated and no longer maintain. Please switch to it's successor: Computed-Types.
Funval is a minimalist validation library that seamlessly integrates with your business logic. Using only plain functions, Funval knows how to validate your data and automatically generates TypeScript interfaces to reduce code duplications and complexity.
Main Features
- Easy to Read - Uses functions as types (including
String
,Number
,Boolean
, etc...) - Reusable Interfaces - Create new validator using plain functions in seconds.
- TypeScript Validation - Detect errors during compile time.
- Function Composition - Pipe multiple validators to generate new ones.
- Asynchronous & Synchronous Support - Automatically detected promises.
- Pure Javascript - Also works without TypeScript.
Install
npm i funval
Usage
; ; ; ; try catch err
Creating Validators
A validator is any function that can return a value without throwing any exceptions:
;
You can use the above validator on schemas as an Email
type:
; ;
Asynchronous Validators
Asynchronous validators are supported by returning a Promise
(or PromiseLike
) values:
;
Funval automatically detects promise and convert the return type of the Validator
to promise as well:
;; ;
If you prefer, you can safely convert any validator to an asynchronous validator using the Async
helper:
; ;; // will catch instead of throwingvalidator.catchconsole.errerr;
Available Validators
All the available validators ordered by type:
-
Schema -
Schema
,Maybe
,Optional
,Default
,Required
,Truthy
,Or
,ArrayOf
,TypeOf
,Any
,Override
Test
-
Comparison -
Equals
,GreaterThan
,GreaterThanEqual
,LessThan
,LessThanEqual
,Between
-
Strings -
ContentString
,TrimString
StringRange
StringMatch
-
Booleans -
Bool
Schema
; declare ;
Validate inputs using the given schema.
Throws: If the input does not match the given schema.
Maybe
; declare ;
Same as Schema
but ignores undefined
or omitted inputs.
Throws: If the input tested and does not match the given schema.
Optional
; declare ;
Same as Schema
but ignores undefined
, null
or omitted inputs.
Throws: If the input tested and does not match the given schema.
Default
; declare ;
Validate inputs using the given schema or use the given value if input equals to undefined
or
omitted.
Throws: If the input tested and does not match the given schema.
Required
; declare ;
Validate inputs using the given schema.
Throws: If input is undefined
or does not match the given schema.
Truthy
; declare ;
Validate input is truthy.
Throws: If input is not truthy.
Or
; declare ;
Validate inputs using any of the given candidates.
Throws: If the input does not match to any of the given schemas.
ArrayOf
; declare ;
Validate the input is an array and that each item matches the given schema.
Throws: If the given input is not an array or one of the items not matches the item schema.
TypeOf
; declare ;
Check the input type is equals to the given type.
Throws: If the input type is different from the given type.
Any
; declare ;
Allow any type of input.
Throws: Never.
Override
; declare ;
Override any input and returns the given value.
Throws: Never.
Test
; declare ;
Return the input value as is, only if the test function returns a truthy value.
Throws: If the test function throws or the test function return a non-truthy value.
Equals
; declare ;
Check the input is strictly equals to the given value.
Throws: If the input does not equal to the given value.
GreaterThan
; declare ;
Check the input is greater than the given value.
Throws: If the input does not greater than the given value.
GreaterThanEqual
; declare ;
Check the input is greater than or equals the given value.
Throws: If the input does not greater than or equals the given value.
LessThan
; declare ;
Check the input is less than the given value.
Throws: If the input does not less than the given value.
LessThanEqual
; declare ;
Check the input is less than or equals the given value.
Throws: If the input does not less than or equals the given value.
Between
; declare ;
Check the input is between the given boundaries.
Throws: If the input is not between the given boundaries.
ContentString
; declare ;
Converts any input to a valid string.
Throws: If input is either null
, undefined
, an object without proper toString
implementation, empty or a whitespace string.
TrimString
; declare ;
Same as ContentString
, but trim
the output as well.
Throws: If input is either null
, undefined
, an object without proper toString
implementation, empty or a whitespace string.
StringRange
; declare ;
Converts any input to a valid string and make sure the string is in the given boundaries.
Throws: If input is either null
, undefined
, an object without proper toString
implementation, empty string, whitespace string, or string outside of the given boundaries.
StringMatch
; declare ;
Validate the input is matches the given regular expression.
Throws: If input does not match the given regular expression.
Float
; declare ;
Converts any input to a valid float number.
Throws: If the input can not convert to a valid number.
Integer
; declare ;
Converts any input to a valid integer number.
Throws: If the input can not convert to a valid number or the number is not an integer.
Bool
; declare ;
Converts any input to a valid boolean.
Throws: If the input can not convert unambiguously to a boolean.