Skip to content

Commit 2b6f9bd

Browse files
committed
feat: different approach with encode in ajv validator
1 parent 18dac92 commit 2b6f9bd

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

packages/core/src/__tests__/ref-utils.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ describe('ref-utils', () => {
168168
expect(escapePointerFragment(123)).toStrictEqual(123);
169169
});
170170

171-
it('should URI-encode percent sign when escaping pointer fragments', () => {
172-
expect(escapePointerFragment('percent%')).toStrictEqual('percent%25');
173-
});
174-
175171
it('should not URI-encode other special characters when escaping pointer fragments per https://datatracker.ietf.org/doc/html/rfc6901#section-6', () => {
176172
expect(escapePointerFragment('curly{braces}')).toStrictEqual('curly{braces}');
177173
expect(escapePointerFragment('plus+')).toStrictEqual('plus+');

packages/core/src/ref-utils.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,18 @@ export class Location {
4141
}
4242

4343
export function unescapePointerFragment(fragment: string): string {
44+
const unescaped = fragment.replace(/~1/g, '/').replace(/~0/g, '~');
45+
4446
try {
45-
return decodeURIComponent(fragment.replaceAll('~1', '/').replaceAll('~0', '~'));
46-
} catch (error) {
47-
return fragment.replaceAll('~1', '/').replaceAll('~0', '~');
47+
return decodeURIComponent(unescaped);
48+
} catch (e) {
49+
return unescaped;
4850
}
4951
}
5052

5153
export function escapePointerFragment<T extends string | number>(fragment: T): T {
5254
if (typeof fragment === 'number') return fragment;
53-
let result: string = fragment;
54-
// add only % escape if % is present
55-
if (result.includes('%')) {
56-
result = result.replaceAll('%', '%25');
57-
}
58-
return result.replaceAll('~', '~0').replaceAll('/', '~1') as T;
55+
return fragment.replaceAll('~', '~0').replaceAll('/', '~1') as T;
5956
}
6057

6158
export function parseRef(ref: string): { uri: string | null; pointer: string[] } {

packages/core/src/rules/ajv.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ function getAjvValidator(
4444
allowAdditionalProperties: boolean
4545
): ValidateFunction | undefined {
4646
const ajv = getAjv(resolve, allowAdditionalProperties);
47+
const $id = encodeURI(loc.absolutePointer);
4748

48-
if (!ajv.getSchema(loc.absolutePointer)) {
49-
ajv.addSchema({ $id: loc.absolutePointer, ...schema }, loc.absolutePointer);
49+
if (!ajv.getSchema($id)) {
50+
ajv.addSchema({ $id, ...schema }, $id);
5051
}
5152

52-
return ajv.getSchema(loc.absolutePointer);
53+
return ajv.getSchema($id);
5354
}
5455

5556
export function validateJsonSchema(

tests/e2e/lint/no-invalid-schema-examples-oas3.1-error/snapshot.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ referenced from openapi.yaml:36:19 at #/paths/~1my_post/post/requestBody/content
3232
Error was generated by the no-invalid-schema-examples rule.
3333

3434

35-
[3] openapi.yaml:44:28 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%25/example
35+
[3] openapi.yaml:44:28 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%/example
3636

3737
Example value must conform to the schema: type must be number.
3838

@@ -43,7 +43,7 @@ Example value must conform to the schema: type must be number.
4343
45 |
4444
46 | responses:
4545

46-
referenced from openapi.yaml:43:19 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%25
46+
referenced from openapi.yaml:43:19 at #/paths/~1my_post/post/requestBody/content/application~1json/schema/properties/three_%
4747

4848
Error was generated by the no-invalid-schema-examples rule.
4949

0 commit comments

Comments
 (0)