Skip to content

Commit 3de8821

Browse files
committed
add readme and fix pattern bug
1 parent fc929cf commit 3de8821

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

packages/scope-validator/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
```

packages/scope-validator/lib/pattern.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export default class Pattern {
2424
let parameterNames = this.str.split(/(?:\${|})/g);
2525
parameterNames = parameterNames.filter((value, index) => index % 2);
2626

27+
if (parameterNames.length !== new Set(parameterNames).size) {
28+
throw new Error('Parameter name must be different');
29+
}
30+
2731
const regexStr = this.str
2832
.replace(/(\${\w+})/g, '*')
2933
.replace(/[$^.+?]/g, '\\$&')

packages/scope-validator/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "scope-validator",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Pattern matching based Oauth2.0 scope validation library",
5-
"main": "dist/index.js",
5+
"main": "dist/lib/index.js",
66
"scripts": {
77
"test": "jest",
88
"build": "npm run clean && npm run compile",
@@ -24,5 +24,5 @@
2424
"ts-jest": "^26.3.0",
2525
"typescript": "^4.0.2"
2626
},
27-
"types": "dist/index.d.ts"
27+
"types": "dist/lib/index.d.ts"
2828
}

packages/scope-validator/test/integration/single-parameter.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const TestValidator3 = ScopeValidatorFactory.create<string>(
2323
(name, { received }) => received === 'test'
2424
);
2525

26+
const TestValidator4 = ScopeValidatorFactory.create(
27+
// eslint-disable-next-line no-template-curly-in-string
28+
'create:${param}:${param}',
29+
() => true
30+
);
31+
2632
describe('success', () => {
2733
it('validate scope', () => {
2834
const scopeValidatorManager = new ScopeValidatorManager();
@@ -93,6 +99,19 @@ describe('success', () => {
9399
});
94100

95101
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+
96115
it('not match ownerId', () => {
97116
const scopeValidatorManager = new ScopeValidatorManager();
98117
scopeValidatorManager.use(TestValidator1);

0 commit comments

Comments
 (0)