Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-type-check

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-type-check - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

dist/index.js

7

package.json
{
"name": "json-type-check",
"version": "0.0.1",
"version": "0.0.2",
"description": "A typechecker for JSON",
"license": "ISC",
"author": "Greg Kops <greg@thinktopography.com>",
"author": "",
"maintainers": [

@@ -15,2 +15,3 @@ {

"scripts": {
"build": "rm -rf ./dist && babel ./src --out-dir ./dist",
"test": "NODE_ENV=test NODE_PATH=./src nyc --reporter=lcov --reporter=text --recursive --require babel-core/register mocha ./src/tests.js"

@@ -22,4 +23,2 @@ },

},
"author": "",
"license": "ISC",
"bugs": {

@@ -26,0 +25,0 @@ "url": "https://github.com/mahaplatform/json-type-check/issues"

# json-type-check
A typechecker for JSON
<a href="https://circleci.com/gh/mahaplatform/json-type-check">
<img src="https://img.shields.io/circleci/project/mahaplatform/json-type-check.svg?maxAge=600" alt="Build Status" >
</a>
<a href="https://codeclimate.com/github/mahaplatform/json-type-check">
<img src="https://img.shields.io/codeclimate/github/mahaplatform/json-type-check.svg?maxAge=600" alt="Code Climate" />
</a>
<a href="https://codeclimate.com/github/mahaplatform/json-type-check/coverage">
<img src="https://img.shields.io/codeclimate/coverage/github/mahaplatform/json-type-check.svg?maxAge=600" alt="Code Coverage" />
</a>
```javascript
import Jst from 'json-type-check'
const ruleset = {
requiedString: [
Jst.string,
Jst.required
],
optionalInteger: Jst.integer,
optionalDate: Jst.date,
optionalEnum: Jst.oneOf([
Jst.integer,
Jst.string
]),
optionalObjectWithShape: Jst.shape([
foo: Jst.integer,
bar: Jst.string,
baz: Jst.sfunc
])
}
const check = new Jst(options, ruleset)
```

@@ -36,13 +36,19 @@ import _ from 'lodash'

return ruleset.reduce((keyErrors, rule) => {
if(_.isArray(ruleset)) {
const ruleErrors = this._checkRule(key, value, rule)
return ruleset.reduce((keyErrors, rule) => {
return [
...keyErrors,
...ruleErrors
]
const ruleErrors = this._checkRule(key, value, rule)
}, [])
return [
...keyErrors,
...ruleErrors
]
}, [])
}
return this._checkRule(key, value, ruleset)
}

@@ -61,121 +67,146 @@

static required = (key, value) => {
static required = (key, value) => {
if(_.isUndefined(value)) return `${key} is required`
if(_.isUndefined(value)) return `${key} is required`
}
}
static string = (key, value) => {
static string = (key, value) => {
if(!_.isString(value)) return `${key} must be a string`
if(_.isUndefined(value)) return value
}
if(!_.isString(value)) return `${key} must be a string`
static number = (key, value) => {
}
if(!_.isNumber(value)) return `${key} must be a number`
static number = (key, value) => {
}
if(_.isUndefined(value)) return value
static integer = (key, value) => {
if(!_.isNumber(value)) return `${key} must be a number`
if(!_.isInteger(value)) return `${key} must be an integer`
}
}
static integer = (key, value) => {
static date = (key, value) => {
if(_.isUndefined(value)) return value
if(!_.isDate(value)) return `${key} must be a date`
if(!_.isInteger(value)) return `${key} must be an integer`
}
}
static boolean = (key, value) => {
static date = (key, value) => {
if(!_.isBoolean(value)) return `${key} must be a boolean`
if(_.isUndefined(value)) return value
}
if(!_.isDate(value)) return `${key} must be a date`
static func = (key, value) => {
}
if(!_.isFunction(value)) return `${key} must be a function`
static boolean = (key, value) => {
}
if(_.isUndefined(value)) return value
static hash = (key, value) => {
if(!_.isBoolean(value)) return `${key} must be a boolean`
if(!_.isPlainObject(value)) return `${key} must be a hash`
}
}
static func = (key, value) => {
static regexp = (key, value) => {
if(_.isUndefined(value)) return value
if(!_.isRegExp(value)) return `${key} must be a regular expression`
if(!_.isFunction(value)) return `${key} must be a function`
}
}
static array = (key, value) => {
static hash = (key, value) => {
if(!_.isArray(value)) return `${key} must be an array`
if(_.isUndefined(value)) return value
}
if(!_.isPlainObject(value)) return `${key} must be a hash`
static oneOf = (rules) => (key, value) => {
}
const valid = rules.reduce((valid, rule) => {
static regexp = (key, value) => {
if(valid) return valid
if(_.isUndefined(value)) return value
const result = rule(key, value)
if(!_.isRegExp(value)) return `${key} must be a regular expression`
return _.isUndefined(result)
}
}, false)
static array = (key, value) => {
if(!valid) return `${key} must be one of ''`
if(_.isUndefined(value)) return value
}
if(!_.isArray(value)) return `${key} must be an array`
static arrayOf = (rules) => (key, value) => {
}
const isArray = Jst.array(key, value)
static oneOf = (rules) => (key, value) => {
if(!_.isUndefined(isArray)) return isArray
if(_.isUndefined(value)) return value
const valid = value.reduce((valid, item) => {
const valid = rules.reduce((valid, rule) => {
if(valid) return valid
if(valid) return valid
const isOneOf = Jst.oneOf(rules)(key, item)
const result = rule(key, value)
return _.isUndefined(isOneOf)
return _.isUndefined(result)
}, false)
}, false)
if(!valid) return `${key} must be an array of ''`
if(!valid) return `${key} must be one of ''`
}
}
static shape = (shape) => (key, hash) => {
static arrayOf = (rules) => (key, value) => {
const isHash = Jst.hash(key, hash)
if(_.isUndefined(value)) return value
if(!_.isUndefined(isHash)) return isHash
const isArray = Jst.array(key, value)
const valid = Object.keys(shape).reduce((valid, subkey) => {
if(!_.isUndefined(isArray)) return isArray
if(!valid) return valid
const valid = value.reduce((valid, item) => {
const value = hash[subkey]
if(valid) return valid
const rule = shape[subkey]
const isValid = (_.isArray(rules)) ? Jst.oneOf(rules)(key, item) : rules(key, item)
const isValid = rule(subkey, value)
return _.isUndefined(isValid)
return _.isUndefined(isValid)
}, false)
}, true)
if(!valid) return `${key} must be an array of ''`
if(!valid) return `${key} must have the shape ''`
}
}
static shape = (shape) => (key, hash) => {
if(_.isUndefined(hash)) return hash
const isHash = Jst.hash(key, hash)
if(!_.isUndefined(isHash)) return isHash
const valid = Object.keys(shape).reduce((valid, subkey) => {
if(!valid) return valid
const value = hash[subkey]
const rule = shape[subkey]
const isValid = rule(subkey, value)
return _.isUndefined(isValid)
}, true)
if(!valid) return `${key} must have the shape ''`
}
}

@@ -182,0 +213,0 @@

@@ -123,3 +123,3 @@ import { expect } from 'chai'

it('accept correct value', () => {
it('accept correct single type value', () => {

@@ -132,4 +132,23 @@ const hash = {

foo: [
Jst.arrayOf(Jst.integer)
]
}
const check = new Jst(hash, schema)
expect(check.result).to.be.true
expect(check.errors).to.be.empty
})
it('accept correct multiple type value', () => {
const hash = {
foo: [1,'a','b',4]
}
const schema = {
foo: [
Jst.arrayOf([
Jst.integer
Jst.integer,
Jst.string
])

@@ -220,5 +239,3 @@ ]

const schema = {
foo: [
rule
]
foo: rule
}

@@ -236,5 +253,3 @@

const schema = {
foo: [
rule
]
foo: rule
}

@@ -241,0 +256,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc