Facebook's PropTypes-alike interface implementation for client and server, reusable and extensible. It produce error reports instead of throwing or printing into console. It works without React.
Table of Contents
- Installation
- Usage
- Examples
- Standard checks
- Non-standard checks
- Decorators
- Checks and groups
- Extension
- API
- License
Installation
Via npm:
npm i typed-props
Or via unpkg.com:
Complete usage guide.
Usage
Custom types check:
const type = Tshape id: TnumberisRequired name: TstringisRequired email: TstringisRequired // Or const type = STshape id: STnumber name: STstring email: STstringoptional const data = id: '1' name: null const issues =
Output
Result of validation is an Array of Issues. Issue is an object which describes validator rules violation.
Issue typing:
Example:
// Violated rule. rule: 'type' // Value location. It helps to receive value from nested object. path: 'user' 'messages' 0 // Details explain what exactly goes wrong. details: // Reason helps to identify kind of problem within one validator. // Usual values are mismatch, no_matches, and redundant. reason: 'mismatch' // The next values are validator dependent. type: 'string' expect: true is: false
Examples
- Create UniqItems check.
- Describe API with TypedProps (with circular types resolution).
Standard checks
Standard checks provided by Facebook's PropTypes:
// Object properties should pass all checks.const shape = Typeshape // Any value except of undefined anything: TypeisRequired // Which is equivalent of anythingElse: TypeanyisRequired // Number property number: Typenumber // String property string: Typestring // Boolean property bool: Typebool // Object property object: Typeobject // Array property array: Typearray // Array property func: Typefunc // Symbol property symbol: Typesymbol // Property which value is instance of Date instanceOf: Type // Complex rules // One of check if value is in list of passed primitives // It works like an enum oneOf: 'one' 'two' // Check if all array values match the passed TypedProps arrayOf: Typenumber // Check if value is matched any of passed TypedProps. oneOfType: Type // Check if all object properties passes the TypedProps. objectOf: Type // Check shape has described properties and no other props. exactShape: Type // Exact shape with custom props exactShape: Type const issues = // => [{path:['anything'], rule: 'isRequired', details: {is: false}}]
Result of check
call is array of issues. If there is no issues, this array will be
empty.
⚠️ If shape/exact rule property presented by function it should return type to check.
⚠️ .object will fail for arrays and vice versa.
Named and circular types
Using named types it's possible to created nested structures with cross references. It could be useful for validating complex data types. Also names help to produce more meaningful error reports.
const store = const userType = Typeshape id: TypenumberisRequired name: TypestringisRequired friends: Type store
Non-standard checks
Typeshape // Make type optional optionalValue: Typeoptional // Determine type in runtime propertyDependentType: Type // Create custom checks when TypedProps is not enough. customValue: Type
TypedProps have it's own custom checks which make it more handful.
Checkable.optional
This is pseudo check. This rule allows to make some property optional. It can
switch off previously defined .isRequired
check. It's better to use with StrictType
.
StrictTypenumberoptional
Checkable.is()
(value:*) -> Checkable
This check validates the checkable value to strict equal value
argument.
Type
Checkable.select()
(...Function|Checkable) -> Checkable
This checker allow to dynamically switch between types depending on input value. It selects the first arguments that is not a function or if it's function and it returns something truly. Then use it as type to check.
Type
Checkable.exactFuzzy()
(shape:ShapeType, fuzzy:...[RegExp, Checkable]) -> Checkable
ShapeType = Object<key,Checkable|Shape>
Check exact shape with fuzzy properties which keys should match regexp from fuzzy params.
const fuzzy = Type
Checkable.custom()
(check:(it:*) -> bool) -> Checkable
Custom check accepts check
argument and use it to validate the value.
Decorators
Experimental Feature. Decorators can check function arguments and value it returns in runtime.
⚠️ Decorators throw CheckError with
issues
property.
// Fixed arguments length example @ @ { return a + b } // Variadic argument's length example @ @ { return numbers }
Checks and groups
Currently there are several groups of checkers: existence, type, exact, and complex checks.
- Existance:
- isRequired
- optional: removes isRequired
- Type:
- bool
- number
- string
- func
- object
- array
- any: removes any other type check
- Exact:
- is
- oneOf
- Complex:
- oneOfType
- arrayOf: overwirites type check with
array
- objectOf: overwirites type check with
object
- shape: replaces exact
- exact: replaces shape
- select
- custom
Type and existence checks are switchable and can replace each other. It's made for flexibility.
TypeisRequirednumberstringfunc // -> final check is "required function".
In the same time the complex checks like shape
or arrayOf
require the input value to has certain
type object and array respectively. They will overwrite type checks too. But overwiting of complex types
currently isn't possible because of unexpected behaviour. It could leads to runtime errors.
So it strongly recommended not to overwrite such checks.
It could be changed in the future.
TypeisRequirednumber // Will throw an error in runtime
Extension
TypedProps support extension throught inheritance. So you can create new type using
extend
on Type class or mixing in new rules with Object.defineProperty
.
More real life example is in repository examples directory.
static ruleName = 'isFinite' static { return } static { return IsFinite } { return isFinite }// Mixin into TypedPropsObject Object
Rule types
There is several Rules which are using internally.
UniqRule
This is probably the most useful type. This rule overwrites any other
occurencies of the rule in type's rules array. It defines it's own
create
method which shouldn't be overriden by custom implementation. Mostly
because it senseless.
SimpleRule
This rule extends UniqRule
and define it's own check
method which take all
work by creating non-check logic (issue createsion, result comparision). It
requires ckeckIt
method wich should return boolean
value.
static ruleName = 'isArray' static { return Array }
API
Checkable()
Checkable is a prototype of TypeProps. It contains only check logic and has no own rules.
Checkable#getChecks()
() -> Checks[]
Returns a list of all defined checks from the TypedProps instance.
check()
(value:*, type:Checkable) -> Array.<Issue>
Validate value
to satisfy type
requirements. Always produce an Array of Issues.
getRuleParams()
(type:TypedProps, name:string) -> null|*[]
type
- Target type.name
- Rule name.=
Returns check params object or null if rule isn't found.
Receive rule
params by its' name.
listRules()
(type:TypedProps) -> string[]
type
- Target type.=
Returns list of rule name in order they were defined.
Rule{}
{
ruleName: string
config: (...args[]) -> RuleParams
create: (options:Array.<Check>, params:RuleParams) -> Array.<Check>
check: (it:*, params:RuleParams, self:Checkable) -> Array.<Issue>
}
Rule is an object which contains several methods required for TypeProps to work:
Rule.config()
(...args:*) -> Object
Receives arguments from call and transform them to Rule internal representation. This representation could be different from rule to rule.
Rule.create()
(checks:Check[], params:object) -> Checks[]
Adds new check into array of already existing checks. It can overwrite any rule defined earlier when it is necessary. Also it can modify a rule defined previously. But in most cases it just remove previously defined rule and push new one to the end.
Rule.check()
CheckFunction
Receives value
and checks that value is satisfying rule logic with params
. If it's
not, then method returns list of found issues.
Issue{}
{ path: Array.<String|Number> rule: String details: Object}
Check{}
{
rule: String,
params: Object
check: CheckFunction
}
CheckFunction()
(value:*, params:Object) -> Issue[]
Object representing validation failure.
License
Copyright © 2018–2019, Rumkin. Released under MIT License.