Skip to content

Commit

Permalink
Fix nested use of type-check and custom types
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
gkz committed Apr 3, 2020
1 parent 33976cc commit b518bbc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
40 changes: 21 additions & 19 deletions lib/check.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 18 additions & 19 deletions src/check.ls
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,56 @@ default-type =
array: 'Array'
tuple: 'Array'

function check-array input, type
all (-> check-multiple it, type.of), input
function check-array input, type, options
all (-> check-multiple it, type.of, options), input

function check-tuple input, type
function check-tuple input, type, options
i = 0
for types in type.of
return false unless check-multiple input[i], types
return false unless check-multiple input[i], types, options
i++
input.length <= i # may be less if using 'Undefined' or 'Maybe' at the end

function check-fields input, type
function check-fields input, type, options
input-keys = {}
num-input-keys = 0
for k of input
input-keys[k] = true
num-input-keys++
num-of-keys = 0
for key, types of type.of
return false unless check-multiple input[key], types
return false unless check-multiple input[key], types, options
num-of-keys++ if input-keys[key]
type.subset or num-input-keys is num-of-keys

function check-structure input, type
function check-structure input, type, options
return false if input not instanceof Object
switch type.structure
| 'fields' => check-fields input, type
| 'array' => check-array input, type
| 'tuple' => check-tuple input, type
| 'fields' => check-fields input, type, options
| 'array' => check-array input, type, options
| 'tuple' => check-tuple input, type, options

function check input, type-obj
function check input, type-obj, options
{type, structure} = type-obj
if type
return true if type is '*' # wildcard
setting = custom-types[type] or types[type]
setting = options.custom-types[type] or types[type]
if setting
(setting.type-of is void or setting.type-of is typeof! input)
and setting.validate input
else
# Booleam, String, Null, Undefined, Error, user defined objects, etc.
type is typeof! input and (not structure or check-structure input, type-obj)
type is typeof! input and (not structure or check-structure input, type-obj, options)
else if structure
return false unless that is typeof! input if default-type[structure]
check-structure input, type-obj
check-structure input, type-obj, options
else
throw new Error "No type defined. Input: #input."

function check-multiple input, types
function check-multiple input, types, options
throw new Error "Types must be in an array. Input: #input." unless typeof! types is 'Array'
any (-> check input, it), types
any (-> check input, it, options), types

var custom-types
module.exports = (parsed-type, input, options = {}) ->
custom-types := options.custom-types or {}
check-multiple input, parsed-type
options.custom-types = {} unless options.custom-types
check-multiple input, parsed-type, options
12 changes: 12 additions & 0 deletions test/check.ls
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ suite 'check' ->
assert c 'str', 'foo', o
assert not c 'str', 1, o

test 'nested check with custom types' ->
o =
custom-types:
A:
type-of: 'Object'
validate: -> c 'String', it.foo and it.foo = 'A'
B:
type-of: 'Object'
validate: -> c 'String', it.bar and it.bar = 'B'

assert c '{a: A, b: B}', {a: {foo: 'A'}, b: {bar: 'B'}}, o

test 'nested' ->
type = '{a: (String, [Number], {x: {a: Maybe Number}, y: Array, ...}), b: Error{message: String, ...}}'
assert c type, {a: ['hi', [1, 2, 3], {x: {a: 42}, y: [1, 'bye']}], b: new Error 'message'}
Expand Down

0 comments on commit b518bbc

Please sign in to comment.