MCPcopy Create free account
hub / github.com/loopbackio/loopback-next / validateApiPath

Function validateApiPath

packages/rest/src/router/openapi-path.ts:22–50  ·  view source on GitHub ↗
(path = '/')

Source from the content-addressed store, hash-verified

20 * modifier, custom pattern, or unnamed parameter is allowed.
21 */
22export function validateApiPath(path = '/') {
23 const tokens = parse(path);
24 for (const token of tokens) {
25 if (typeof token === 'string') continue;
26 if (typeof token === 'object') {
27 const name = token.name;
28 if (typeof name === 'string' && name !== '') {
29 throw new Error(
30 `Invalid path template: '${path}'. Please use {${name}} instead of ':${name}'`,
31 );
32 }
33 if (typeof name === 'number') {
34 throw new Error(`Unnamed parameter is not allowed in path '${path}'`);
35 }
36 const valid = token.prefix.match(VALID_VARNAME_PATTERN);
37 if (!valid) {
38 throw new Error(
39 `Invalid parameter name '${token.prefix}' found in path '${path}'`,
40 );
41 }
42 if (['?', '+', '*'].includes(token.modifier)) {
43 throw new Error(
44 `Parameter modifier '{${token.prefix}}${token.modifier}' is not allowed in path '${path}`,
45 );
46 }
47 }
48 }
49 return path;
50}
51
52/**
53 * Get all path variables. For example, `/root/{foo}/bar` => `['foo']`

Callers 3

registerRouteMethod · 0.90
disallowsFunction · 0.85

Calls 1

matchMethod · 0.65

Tested by 1

disallowsFunction · 0.68