@stackhouseos/json-rules
v1.2.9
Published
Validate json business logic
Downloads
223
Readme
Json rules
- use rules field
- accept array or object values
- key is field formkey
- value check condition
- true is present
- false is not valid or empty
- string is equal
- array is include
- prefix options
- checkAll options
- ^ for skip prefix
Code
import jrules from '@stackhouseos/json-rules'
const [hasError, results] = jrules({a: {eq: 'b'}}, {a: 'b'}, false)
Examples fields
Check is field status equal PENDING
{
"rules":{
"status": { in: ["PENDING"] }
}
}
Check is field deleted equal true
{
"rules":{
"deleted": {eq: true}
}
}
Check is field deleted equal true AND status equal PENDING
{
"rules": [
{"deleted": {eq: true} },
{"status": { in: ["PENDING"] } }
]
}
Check is field deleted equal true AND status equal PENDING AND guid is present
{
"rules": {
and : [
{
"status": { in: ["PENDING"] } }
},
{
"guid": {eq: true}
}
]
}
}
Check is field deleted equal true OR status equal PENDING OR guid is present
{
"rules": {
or: [
{
"status": { in: ["PENDING"] }
},
{
"guid": {eq: true}
}
]
}
}
Advance
{
rules: {
or: [
{ 'form.radio': true },
{
and: [
{ 'form.name': { min: 5, name: 'ERROR-MIN' } },
{ 'form.name': { eq: { ref: 'form.city' } } },
{ 'form.name': { len: { ref: 'form.city' } } },
{ 'form.name': { len: { ref: 'form.city', transform: val => val+1 } } }
]
},
{
'form.name': (val) => {
return val === 'aaaa'
}
},
]
}
}
Valid Operators
ex: Esiste
nex: Non esiste
nu: (object not exist or false)
nnu: not (object not exist or false),
eq: uguale ===
neq: non uguale ===
weq: uguale ==
nweq: non uguale ==
gt: più grande,
gte: più grande o uguale,
lt: più piccolo,
lte: più piccolo o uguale
mi: dimensione array pià piccola di
ma: dimensione array pià grande di,
min: dimensione array pià piccola di o uguale,
max: dimensione array pià grande di o uguale,
len: dimensione array uguale
in: include,
nin: non include
all: include tutti,
nall: non include tutti
find: cerco,
con: contiene insenstive,
coni: contiene senstive
ncon: non contiene,
re: regex
nre: not regex
func: funzione
tof: typeof uguale