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

obey

Package Overview
Dependencies
Maintainers
3
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obey - npm Package Compare versions

Comparing version 4.0.2 to 4.1.0

src/lib/plugins.js

6

CHANGELOG.md

@@ -0,1 +1,7 @@

### v4.1.0 (2019-06-19):
* Added `jexl` validator method for evaluating data against [Jexl](https://github.com/TomFrost/Jexl) expressions.
* Added root `use` method for adding plugin references to external packages. (Currently only used by `jexl` validator.)
### v4.0.2 (2019-06-01):

@@ -2,0 +8,0 @@

3

package.json
{
"name": "obey",
"version": "4.0.2",
"version": "4.1.0",
"description": "Data modelling and validation library",

@@ -44,4 +44,5 @@ "main": "src/index.js",

"dot-object": "^1.7.1",
"jexl": "^2.1.1",
"lodash": "^4.17.11"
}
}

@@ -168,2 +168,3 @@ # Obey

* `description`: A description of the property
* `jexl`: One or more objects containing an `expr` string for validating data against Jexl expressions. See [Jexl Validation](#jexl-validation).

@@ -334,2 +335,45 @@ ## Types

## Jexl Validation
Obey allows for validating data against [Jexl](https://github.com/TomFrost/Jexl) expressions via the `jexl` rule:
```javascript
obey.model({
exprVal: {
type: 'string',
jexl: [{
expr: "value == root.testVal.nestedObjArray[.name == 'some specific name'].payload",
message: "Do not seek the treasure" // Optional expression-specific error message
}]
},
testVal: {
type: 'object',
keys: {
nestedObjArray: {
type: 'array',
values: {
type: 'object',
keys: {
name: { type: 'string' },
payload: { type: 'string' }
}
}
}
}
}
})
```
**Note**: the `jexl` validator uses `value` and `root` as its context keys for the specific value being validated and the corresponding data, respectively, so expression strings should be constructed accordingly.
If necessary, a preconfigured Jexl instance can be passed in before the model is constructed, in order to utitlize user-defined transforms in validation:
```javascript
const obey = require('obey')
const jexl = require('jexl')
jexl.addTransform('upper', val => val.toUpperCase(val))
obey.use('jexl', jexl)
obey.model({/* ...definition... */})
```
## Asynchronous Validation

@@ -336,0 +380,0 @@

@@ -11,2 +11,3 @@ /*

const ValidationError = require('./lib/error')
const plugins = require('./lib/plugins')

@@ -63,3 +64,12 @@ /**

*/
creator: (name, fn) => creators.add(name, fn)
creator: (name, fn) => creators.add(name, fn),
/**
* Adds given package to plugins lib.
*
* @param {string} name The package name
* @param {function|Object} pkg The package reference
*/
// TODO: make this actually useful
use: (name, pkg) => plugins.add(name, pkg)
}

@@ -6,2 +6,4 @@ /*

const dot = require('dot-object')
const jexl = require('jexl')
const plugins = require('./plugins')

@@ -163,2 +165,31 @@ const validators = {

}
},
/**
* Validator jexl method, used by model
* @param {Object} def The property configuration
* @param {*} value The value being validated
* @param {string} key The key name of the property
* @param {Array<{type: string, sub: string|number, key: string, value: *, message: string}>} errors An error array
* to which any additional error objects will be added
* @param {Object} data The full initial data object
*/
jexl: (def, value, key, errors, data) => {
const type = 'jexl'
const sub = Array.isArray(def.jexl) ? def.jexl : [ def.jexl ]
const promises = sub.map((obj) => {
const {
expr,
message = 'Value failed Jexl evaluation'
} = obj
const instance = plugins.lib.jexl || jexl
return instance.eval(expr, { root: data, value })
.then(val => {
if (!val) errors.push({ type, sub: obj, key, value, message })
})
.catch(() => {
errors.push({ type, sub: obj, key, value, message })
})
})
Promise.all(promises)
}

@@ -165,0 +196,0 @@ }

@@ -28,3 +28,4 @@ /*

requireIfNot: { name: 'requireIfNot', fn: validators.requireIfNot },
equalTo: { name: 'equalTo', fn: validators.equalTo }
equalTo: { name: 'equalTo', fn: validators.equalTo },
jexl: { name: 'jexl', fn: validators.jexl }
}

@@ -56,3 +57,4 @@

allProps.requireIfNot,
allProps.equalTo
allProps.equalTo,
allProps.jexl
],

@@ -68,3 +70,4 @@ // No value/undefined

allProps.requireIfNot,
allProps.equalTo
allProps.equalTo,
allProps.jexl
],

@@ -71,0 +74,0 @@ // No value, partial

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