input-filter
Advanced tools
Comparing version 0.0.11 to 0.0.12
{ | ||
"name": "input-filter", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "InputFilter js implementation", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
import FilterChain from './FilterChain.js' | ||
import ValidatorChain from './ValidatorChain.js' | ||
import Eventable from './Eventable.js' | ||
class Input { | ||
class Input extends Eventable { | ||
constructor(name) { | ||
super() | ||
this.required = true | ||
this.name = name | ||
this.value = null | ||
this.valid = null | ||
this.filterChain = new FilterChain() | ||
this.validatorChain = new ValidatorChain() | ||
this.context = {} | ||
} | ||
@@ -25,9 +27,9 @@ | ||
this.value = this.filterChain.filter(value) | ||
//Reset Validator for validate data again | ||
this.validatorChain.promise = null | ||
this.promise = null | ||
return this | ||
} | ||
getValue(value) { | ||
getValue() { | ||
return this.value | ||
@@ -37,24 +39,27 @@ } | ||
isValid(context = {}) { | ||
this.context = context | ||
if (this.promise) { | ||
return this.promise | ||
} | ||
if ((!this.getValue() || this.getValue() == "") && !this.required) { | ||
return true | ||
} | ||
return this.validatorChain.isValid(this.getValue(), context, this.name).catch((messages) => { | ||
throw { | ||
[this.name]: messages | ||
} | ||
}) | ||
} | ||
getMessages() { | ||
return new Promise((resolve, reject) => { | ||
this.validatorChain.isValid(this.getValue(), this.context).then( | ||
this.promise = this.validatorChain.isValid(this.getValue(), context, this.name) | ||
.then( | ||
() => { | ||
resolve([]) | ||
this.valid = true | ||
this.trigger('valid', this) | ||
return { | ||
[this.name]: this.getValue() | ||
} | ||
}, | ||
(messages) => { | ||
resolve(messages) | ||
messages => { | ||
this.valid = false | ||
this.messages = messages | ||
this.trigger('invalid', this) | ||
throw { | ||
[this.name]: messages | ||
} | ||
} | ||
) | ||
}) | ||
return this.promise | ||
} | ||
@@ -61,0 +66,0 @@ } |
@@ -5,6 +5,9 @@ import Input from './Input.js' | ||
import PromiseHelper from './PromiseHelper.js' | ||
import Eventable from './Eventable.js' | ||
class InputFilter { | ||
class InputFilter extends Eventable { | ||
constructor() { | ||
constructor(name = null) { | ||
super() | ||
this.name = name | ||
this.messages = {} | ||
@@ -28,5 +31,22 @@ this.inputs = {} | ||
get(name) { | ||
return this.inputs[name] | ||
} | ||
setValue(data) { | ||
return this.setData(data) | ||
} | ||
getValue(key = null) { | ||
if (key) { | ||
return this.get(key).getValue() | ||
} else { | ||
return this.data | ||
} | ||
} | ||
setData(data) { | ||
this.rawData = data | ||
this.reset() | ||
this.populate() | ||
@@ -36,2 +56,8 @@ return this | ||
populate() { | ||
Object.keys(this.inputs).forEach(name => { | ||
this.data[name] = this.get(name).setValue(this.rawData[name]).getValue() | ||
}) | ||
} | ||
getData() { | ||
@@ -52,9 +78,24 @@ return this.data | ||
} | ||
let promises = Object.keys(this.inputs).map(name => { | ||
let input = this.inputs[name] | ||
this.data[name] = input.setValue(this.rawData[name]).getValue() | ||
return input.isValid(this.data) | ||
}) | ||
let promises = Object.keys(this.inputs) | ||
.map(name => this.get(name).isValid(this.data)) | ||
this.promise = PromiseHelper.assignRejects(promises) | ||
this.promise = PromiseHelper.assignRejects(promises).then( | ||
result => { | ||
this.valid = true | ||
this.trigger('valid', this) | ||
return this.data | ||
}, | ||
messages => { | ||
this.valid = false | ||
this.messages = messages | ||
this.trigger('invalid', this) | ||
if (this.name) { | ||
throw { | ||
[this.name]: messages | ||
} | ||
} else { | ||
throw messages | ||
} | ||
} | ||
) | ||
@@ -64,6 +105,2 @@ return this.promise | ||
getValue(key) { | ||
return this.inputs[key].getValue() | ||
} | ||
static factory(items) { | ||
@@ -73,2 +110,6 @@ let inputFilter = new InputFilter | ||
if (items.hasOwnProperty(name)) { | ||
if (items[name] instanceof Input || items[name] instanceof InputFilter) { | ||
inputFilter.add(items[name]) | ||
continue | ||
} | ||
let input = new Input(name) | ||
@@ -75,0 +116,0 @@ let {validators, filters, required} = items[name] |
@@ -94,3 +94,38 @@ import {assert} from 'chai' | ||
}) | ||
it('should be able to use inner InputFilter', () => { | ||
let fooBarFilter = InputFilter.factory({ | ||
foo: { | ||
validators: [new StringLength({min:3})], | ||
filters: ['StringTrim'] | ||
}, | ||
bar: new FooBarFilter('bar') | ||
}) | ||
fooBarFilter.setData({foo: " 123", bar: {foo: "1234", bar: 5}}) | ||
return fooBarFilter.isValid().then( | ||
data => { | ||
assert.equal(data.foo, "123") | ||
assert.equal(data.bar.bar, 5) | ||
} | ||
) | ||
}) | ||
it('should validate inner InputFilter', () => { | ||
let fooBarFilter = InputFilter.factory({ | ||
foo: { | ||
validators: [new StringLength({min:3})], | ||
filters: ['StringTrim'] | ||
}, | ||
bar: new FooBarFilter('bar') | ||
}) | ||
fooBarFilter.setData({foo: " 123", bar: {foo: "1234", bar: 2}}) | ||
return fooBarFilter.isValid().then( | ||
data => { | ||
throw new Error('cannot be valid') | ||
}, | ||
messages => { | ||
assert.property(messages, "bar") | ||
assert.property(messages.bar, "bar") | ||
} | ||
) | ||
}) | ||
}) | ||
}) |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
30207
35
856
1