aqours
v2.1.0
Published
data filter preventing front end error
Downloads
7
Readme
aqours
filter and detect data type
installing
npm i aqours -S
usage
import aqours from "aqours"
in interceptor
let structure = {
alertType: {
type: "String",
defaultVal: "紧急"
},
startTime: {
custom: value => value < new Date().valueOf(),
defaultVal: new Date().setHours(0).valueOf()
},
department: {
type: "Object", // buxuyao?
child: {
description: {
type: "String"
},
id: {
type: "Number",
defaultVal: 0
}
}
},
solutions: {
type: "Array",// buxuyao?
length: length => length >= 5,
child: {
type: "Number"
}
},
remarks: {
type: "Array",// buxuyao?
length: length => length === 15,
child: {
type: "Object",
child: {
// shortcut for type and no more config
message: "String"
}
}
},
users: [{
// shortcut for elementType "Object" and no length limit
CNName: {
custom: name => !/^a-zA-Z$/.test(name),
defaultVal: "中文名"
}
}]
}
let rules = [{
url: /events$/,
strict: true,
structure
}, {
match: config => /events$/.test(config.url),
structure
}]
let interceptor = function(response) {
let {status, data, config} = response
if (status === 200) {
response.data = aqours(rules, config, data)
}
}
document
to be continued.
api
rules
filter data in interceptor
url
: regular expression, matchconfig.url
match
: function (response), return whether single rule matches the structurestrict
: boolean, optional, if true, aqours will remove redundancystructure
: object, aqours will filter response according to the structure
structure
type
: array / string [string:[String, Number, Date, Object, Array]], when object aqours will recursive into child objectcustom
: function, was first priority if has custom validate type will be ignored, weather value was validatedefaultVal
: mixed, default to "invalid value", when type verify failed the origin value will be replaced by default value