File tree 4 files changed +60
-3
lines changed
4 files changed +60
-3
lines changed Original file line number Diff line number Diff line change
1
+ # Scope Validator
2
+ Pattern matching based Oauth2.0 scope validation library
3
+
4
+ [ ![ CodeFactor] ( https://www.codefactor.io/repository/github/CourseDesign/scope-validator/badge )] ( https://www.codefactor.io/repository/github/CourseDesign/scope-validator )
5
+
6
+ # Installation
7
+ ``` shell script
8
+ $ npm install scope-validator
9
+ ```
10
+
11
+ # Documentation
12
+ [ Documentation / scope-validator] ( https://github.com/CourseDesign/scope-validator/wiki/Documentation%3A-scope-validator )
13
+
14
+ # Example
15
+ ``` typescript
16
+ import { ScopeValidatorFactory , ScopeValidatorManager } from ' scope-vallidator'
17
+
18
+ const ParameterValidator = ScopeValidatorFactory .create (
19
+ ' create_test:${custom_param}' ,
20
+ (name : string , { parameters }) => {
21
+ const { custom_param } = parameters ;
22
+ if (custom_param === ' hello' ) {
23
+ return true
24
+ }
25
+
26
+ return false
27
+ }
28
+ )
29
+
30
+ const validatorManager = new ScopeValidatorManager ();
31
+ validatorManager .use (ParameterValidator );
32
+
33
+ validatorManager .validate ([' create_test:hello' ]);
34
+ ```
Original file line number Diff line number Diff line change @@ -24,6 +24,10 @@ export default class Pattern {
24
24
let parameterNames = this . str . split ( / (?: \$ { | } ) / g) ;
25
25
parameterNames = parameterNames . filter ( ( value , index ) => index % 2 ) ;
26
26
27
+ if ( parameterNames . length !== new Set ( parameterNames ) . size ) {
28
+ throw new Error ( 'Parameter name must be different' ) ;
29
+ }
30
+
27
31
const regexStr = this . str
28
32
. replace ( / ( \$ { \w + } ) / g, '*' )
29
33
. replace ( / [ $ ^ . + ? ] / g, '\\$&' )
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " scope-validator" ,
3
- "version" : " 1.0.0 " ,
3
+ "version" : " 1.0.1 " ,
4
4
"description" : " Pattern matching based Oauth2.0 scope validation library" ,
5
- "main" : " dist/index.js" ,
5
+ "main" : " dist/lib/ index.js" ,
6
6
"scripts" : {
7
7
"test" : " jest" ,
8
8
"build" : " npm run clean && npm run compile" ,
24
24
"ts-jest" : " ^26.3.0" ,
25
25
"typescript" : " ^4.0.2"
26
26
},
27
- "types" : " dist/index.d.ts"
27
+ "types" : " dist/lib/ index.d.ts"
28
28
}
Original file line number Diff line number Diff line change @@ -23,6 +23,12 @@ const TestValidator3 = ScopeValidatorFactory.create<string>(
23
23
( name , { received } ) => received === 'test'
24
24
) ;
25
25
26
+ const TestValidator4 = ScopeValidatorFactory . create (
27
+ // eslint-disable-next-line no-template-curly-in-string
28
+ 'create:${param}:${param}' ,
29
+ ( ) => true
30
+ ) ;
31
+
26
32
describe ( 'success' , ( ) => {
27
33
it ( 'validate scope' , ( ) => {
28
34
const scopeValidatorManager = new ScopeValidatorManager ( ) ;
@@ -93,6 +99,19 @@ describe('success', () => {
93
99
} ) ;
94
100
95
101
describe ( 'failed' , ( ) => {
102
+ it ( 'same parameter name' , ( ) => {
103
+ const scopeValidatorManager = new ScopeValidatorManager ( ) ;
104
+ scopeValidatorManager . use ( TestValidator4 ) ;
105
+
106
+ expect ( ( ) => {
107
+ scopeValidatorManager . validate ( [
108
+ 'create:client:all' ,
109
+ 'create:client:me' ,
110
+ 'create:client:other' ,
111
+ ] ) ;
112
+ } ) . toThrow ( Error ) ;
113
+ } ) ;
114
+
96
115
it ( 'not match ownerId' , ( ) => {
97
116
const scopeValidatorManager = new ScopeValidatorManager ( ) ;
98
117
scopeValidatorManager . use ( TestValidator1 ) ;
You can’t perform that action at this time.
0 commit comments