core.check¶| Stability: | 1 - Experimental |
|---|---|
| Bug Tracker: | https://github.com/folktale/core.check/issues |
| Version: | 0.1.0 |
| Repository: | https://github.com/folktale/core.check |
| Portability: | Portable |
| npm package: | core.check |
Interface checking for JS values.
JavaScript is an untyped language, and this makes it fairly flexible for
certain things. More often than not, however, you want to make sure that the
values going into a certain code path have some kind of structure, to reduce
the complexity of the whole program. core.check helps you to do this by
providing composable contracts:
1 2 3 4 5 | check.assert(check.String(1))
// => Error: Expected 1 to have tag String
check.assert(check.Or([check.String, check.Boolean])(1))
// => Error: Expected 1 to have tag String, or 1 to have tag Boolean
|
core.check can also be used for validating data structures without
crashing the process. All contracts return a Validation(Violation, α)
result. One can then use the cata()
operation on the data.validation.Validation object to deal with the
result of the operation:
1 2 3 4 5 6 7 8 9 10 11 12 | function logString(a) {
return check.String(a).cata({
Failure: function(error){ return 'Not a string: ' + a },
Success: function(value){ return 'String: ' + value }
})
}
logString(1)
// => 'Not a string: 1'
logString('foo')
// => 'String: foo'
|
core.check.Or(interfaces)¶| Returns: | An interface that matches any of the given interfaces. |
|---|
Array(α → Validation(Violation, α)) → α → Validation(Violation, α)
core.check.And(interfaces)¶| Returns: | An interface that matches only if all of the given interfaces match. |
|---|
Array(α → Validation(Violation, α)) → α → Validation(Violation, α)
core.check.Seq(interfaces)¶| Returns: | An interface that matches an N-Tuple with the given interfaces. |
|---|
Array( α₁ → Validation<Violation, α₁)
, α₂ → Validation(Violation, α₂)
, ...
, αₙ → Validation(Violation, αₙ)>
→ Array(α₁, α₂, ..., αₙ)
→ Validation(Violation, Array(α₁, α₂, ..., αₙ))
core.check.Null(aValue)¶Any → Validation(Violation, Any)
An interface that matches only null values.
core.check.Undefined(aValue)¶Any → Validation(Violation, Any)
An interface that matches only undefined values.
core.check.Boolean(aValue)¶Any → Validation(Violation, Any)
An interface that matches only Boolean values.
core.check.Number(aValue)¶Any → Validation(Violation, Any)
An interface that matches only Number values.
core.check.String(aValue)¶Any → Validation(Violation, Any)
An interface that matches only String values.
core.check.Function(aValue)¶Any → Validation(Violation, Any)
An interface that matches only Function values.
core.check.Array(aValue)¶Any → Validation(Violation, Any)
An interface that matches only Array values.