deep-type
a type validator for objects and arrays using GraphQL-ish queries
Last updated 5 years ago by benjamin_st .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install deep-type 
SYNC missed versions from official npm registry.

Deep Type

JS GraphQL-ish Type Checker

Use a GraphQL-esque syntax to validate Javascript objects and arrays. Also tells you where you messed up.

npm install deep-type

Examples

checker = require './checker.coffee'

objectToValidate = {
	key1: {
		innerKey: 'value'
	}
}

checker('{key1: {innerKey}}', objectToValidate)
=> {isValid: true, stack: []}

checker('{key1: {innerKey: number}}', objectToValidate)
=> {isValid: false, stack: ['key1', 'innerKey']}

arrayToValidate = [
	{
		someKey: 'val'
		another: 'val'
	}
	{
		someKey: 'val'
	}
]

checker('[ {someKey, another} ]', objectToValidate)
=> {isValid: false, stack: [0, 'another']}

Usage

checker(query, objectOrArrayToValidate) returns an object that looks like { isValid: true of false stack: a list of keys/indices }

Say the part that failed was object[0]['key']['innerKey'], then stack would be [0, 'key', 'innerKey']

If you only provide the query, you get back a partially applied function.

query = checker('{key}') query({'key': 'value'}) => { isValid: true, stack: [ ] }

Writing Queries


someType => the input is of type 'someType'

{} => the input is an object

{key1, key2, key3} => the input has the following keys: key1, key2, key3

{key1, key2:{innerKey}} => input['key2'] has the following keys: innerKey

{ firstKey: string } => input['firstKey'] is a string

[] => the input is an arrays

[ innerQuery ] => each of the array's elements matches the innerQuery

[ firstQuery, secondQuery ] => the array has exactly two elements. the first element matches firstQuery. the second element matches secondQuery

[ {someKey} ] => each element of the array is an object that has the key 'someKey'

[ string, number ] => the first element of the array is a string. the second element is a number

Current Tags

  • 1.0.2                                ...           latest (5 years ago)

3 Versions

  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (7)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |