validate-scope

Checks whether a subset is contained in a list of scopes. Uses code generation

Usage no npm install needed!

<script type="module">
  import validateScope from 'https://cdn.skypack.dev/validate-scope';
</script>

README

validate-scope

Checks whether a subset is contained in a list of scopes. Uses code generation to reach better performance.

var validateScope = require('validate-scope')

// Check only one scope
var 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
var 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
var validate = validateScope('first && second && !third')
validate(['first']) // returns false
validate(['first', 'second']) // returns true
validate(['first', 'second', 'third']) // returns false

Api

var validateScope = require('validate-scope')
var validate = validateScope(array|string)
validate(array|string) // returns boolean
var 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