https://github.com/marcbachmann/validate-scope
Checks whether a subset is contained in a list of oauth scopes. Uses code generation to reach better performance.
https://github.com/marcbachmann/validate-scope
boolean-expression jwt oauth scope subset validation
Last synced: 7 months ago
JSON representation
Checks whether a subset is contained in a list of oauth scopes. Uses code generation to reach better performance.
- Host: GitHub
- URL: https://github.com/marcbachmann/validate-scope
- Owner: marcbachmann
- Created: 2016-11-09T18:30:03.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T17:33:06.000Z (about 2 years ago)
- Last Synced: 2025-07-03T22:03:53.951Z (7 months ago)
- Topics: boolean-expression, jwt, oauth, scope, subset, validation
- Language: JavaScript
- Homepage:
- Size: 54.7 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# validate-scope
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fmarcbachmann%2Fvalidate-scope?ref=badge_shield)
Checks whether a subset is contained in a list of scopes.
Uses code generation to reach better performance.
```js
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
```js
const validateScope = require('validate-scope')
```
```js
const validate = validateScope(array|string)
validate(array|string) // returns boolean
```
```js
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
```bash
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
```
## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fmarcbachmann%2Fvalidate-scope?ref=badge_large)