validate-scope
v1.3.0
Published
Checks whether a subset is contained in a list of scopes. Uses code generation
Downloads
5,174
Maintainers
Readme
validate-scope
Checks whether a subset is contained in a list of scopes. Uses code generation to reach better performance.
const validateScope = require('validate-scope')
// Check only one scope
const validate = validateScope(['user:edit'])
// pass an array
validate(['profile', 'user:edit']) // returns true
validate(['profile', 'another-scope']) // returns false
// or a string of scopes separated by whitespaces
validate('profile user:edit user:archive') // returns true
// or check multiple scopes
const validate = validateScope('user:edit AND user:archive')
validate('profile user:edit') // returns false
validate('profile user:edit user:archive') // returns true
// you can use more complex boolean expressions
const validate = validateScope('first && second && !third')
validate(['first']) // returns false
validate(['first', 'second']) // returns true
validate(['first', 'second', 'third']) // returns false
Api
const validateScope = require('validate-scope')
const validate = validateScope(array|string)
validate(array|string) // returns boolean
const validate = validateScope('(user:edit AND user:archive) OR admin)')
validate.scopes
// ['user:edit', 'user:archive', 'admin']
I suggest you to save your scopes as array and also pass that to this validation method. String operations are quite slow.
Benchmark
100000000 iterations each
NANOBENCH version 1
# validate("some space separate scopes")
end ~3.51 s (3 s + 513290216 ns)
# validate(["some", "scopes", "as", "array"])
end ~599 ms (0 s + 598547467 ns)
# traditional string split & array indexOf
end ~8.58 s (8 s + 583230771 ns)
# traditional for loop & array indexOf
end ~694 ms (0 s + 693684995 ns)
# total ~13 s (13 s + 388753449 ns)
# ok