-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(matching): add isIdentical predicate (#157)
The isIdentical predicate determines if two paths are identical and thus invalid. Refs #98
- Loading branch information
Showing
6 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "openapi-path-templating", | ||
"version": "2.0.2", | ||
"description": "OpenAPI Path Templating parser, validator and resolver.", | ||
"description": "OpenAPI Path Templating parser, validator, resolver and matcher.", | ||
"main": "./cjs/index.cjs", | ||
"types": "./types/index.d.ts", | ||
"exports": { | ||
|
@@ -41,7 +41,9 @@ | |
"templating", | ||
"parser", | ||
"validator", | ||
"resolver" | ||
"resolver", | ||
"matcher", | ||
"matching" | ||
], | ||
"author": "Vladimír Gorej <[email protected]>", | ||
"license": "Apache-2.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import parse from './parse/index.js'; | ||
|
||
export const isIdentical = (pathTemplate1, pathTemplate2) => { | ||
if (typeof pathTemplate1 !== 'string') return false; | ||
if (typeof pathTemplate2 !== 'string') return false; | ||
|
||
const parseResult1 = parse(pathTemplate1); | ||
const parseResult2 = parse(pathTemplate2); | ||
const parts1 = []; | ||
const parts2 = []; | ||
|
||
if (!parseResult1.result.success) return false; | ||
if (!parseResult2.result.success) return false; | ||
|
||
parseResult1.ast.translate(parts1); | ||
parseResult2.ast.translate(parts2); | ||
|
||
if (parts1.length !== parts2.length) return false; | ||
|
||
for (let i = 1; i < parts1.length; i++) { | ||
const [type1, value1] = parts1[i]; | ||
const [type2, value2] = parts2[i]; | ||
|
||
if (type1 !== type2) return false; | ||
if (type1 === 'template-expression' || type1 === 'template-expression-param-name') continue; | ||
if (value1 !== value2) return false; | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { assert } from 'chai'; | ||
|
||
import { isIdentical } from '../src/predicates.js'; | ||
|
||
describe('predicates', function () { | ||
context('isIdentical', function () { | ||
specify('should consider path templates identical', function () { | ||
assert.isTrue(isIdentical('/pets/{petId}', '/pets/{name}')); | ||
assert.isTrue(isIdentical('/pets/{petId}/owners/{ownerId}', '/pets/{id}/owners/{id}')); | ||
assert.isTrue(isIdentical('/pets/static/segment', '/pets/static/segment')); | ||
assert.isTrue(isIdentical('/{entity}/{id}', '/{resource}/{key}')); | ||
assert.isTrue(isIdentical('/pets/{petId}/', '/pets/{name}/')); | ||
}); | ||
|
||
specify('should not consider path templates identical', function () { | ||
assert.isFalse(isIdentical('/', '/{petId}')); | ||
assert.isFalse(isIdentical('/pets/{petId}', '/animals/{name}')); | ||
assert.isFalse(isIdentical('/pets/{petId}', '/pets/{petId}/owners/{ownerId}')); | ||
assert.isFalse(isIdentical('/pets/static/segment', '/pets/different/segment')); | ||
assert.isFalse(isIdentical('/pets/{petId}/', '/pets/{petId}')); | ||
assert.isFalse(isIdentical('/pets/{petId}/owners/{id}', '/pets/{petId}/owners/static')); | ||
}); | ||
|
||
specify('should return false on invalid path templates', function () { | ||
assert.isFalse(isIdentical('/a', 'b')); | ||
assert.isFalse(isIdentical('a', '/b')); | ||
assert.isFalse(isIdentical('a', 'b')); | ||
assert.isFalse(isIdentical('', '/')); | ||
}); | ||
|
||
specify('should handle complex cases correctly', function () { | ||
assert.isTrue(isIdentical('/pets/{petId}/owners/{ownerId}', '/pets/{id}/owners/{id}')); | ||
assert.isTrue(isIdentical('/{entity}/{id}/{action}', '/{resource}/{key}/{operation}')); | ||
assert.isFalse(isIdentical('/pets/{petId}/owners/{ownerId}', '/pets/{id}/{ownerId}')); | ||
assert.isFalse(isIdentical('/pets/{petId}/{ownerId}', '/pets/{id}/{id}/actions')); | ||
assert.isFalse(isIdentical('/pets/{petId}/{ownerId}', '/pets/{petId}/{ownerId}/actions')); | ||
}); | ||
|
||
specify('should handle non-string inputs gracefully', function () { | ||
assert.isFalse(isIdentical(123, 456)); | ||
assert.isFalse(isIdentical({}, {})); | ||
assert.isFalse(isIdentical([], [])); | ||
assert.isFalse(isIdentical(1, 2)); | ||
assert.isFalse(isIdentical(null, '/b')); | ||
assert.isFalse(isIdentical(undefined, undefined)); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters