-
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.
- Loading branch information
Showing
5 changed files
with
61 additions
and
62 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
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
55 changes: 55 additions & 0 deletions
55
src/config/normalize/lib/wild_wild_parser/validate/path.js
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,55 @@ | ||
import { getPathObjectTokenType } from '../tokens/main.js' | ||
|
||
import { throwQueryError, throwTokenError } from './throw.js' | ||
|
||
// Transform a queryArrays into a path, if possible | ||
// Paths are a subset of query strings|arrays which use: | ||
// - No unions | ||
// - Only prop and index tokens (positive only) | ||
// Those are the ones exposed in output, as opposed to query arrays which are | ||
// exposed in input. | ||
export const normalizeArraysPath = function (queryArrays, query) { | ||
if (queryArrays.length !== 1) { | ||
throwQueryError(query, 'It must not be a union.') | ||
} | ||
|
||
const [path] = queryArrays | ||
return normalizeArrayPath(path, query) | ||
} | ||
|
||
// Ensure a queryArray is a path | ||
export const normalizeArrayPath = function (path, query) { | ||
if (!Array.isArray(path)) { | ||
throwQueryError(query, 'It must be an array.') | ||
} | ||
|
||
if (path.some(Array.isArray)) { | ||
throwQueryError(query, 'It must not be a union.') | ||
} | ||
|
||
path.forEach((prop) => { | ||
validateProp(prop, query) | ||
}) | ||
return path | ||
} | ||
|
||
const validateProp = function (prop, query) { | ||
const tokenType = getPathObjectTokenType(prop) | ||
|
||
if (tokenType === undefined) { | ||
throwTokenError( | ||
query, | ||
prop, | ||
'It must be a property name or an array index.', | ||
) | ||
} | ||
|
||
if (isNegativeIndex(tokenType, prop)) { | ||
throwTokenError(query, prop, 'It must not be a negative index.') | ||
} | ||
} | ||
|
||
// Negative indices are not allowed in paths | ||
const isNegativeIndex = function (tokenType, prop) { | ||
return tokenType.name === 'index' && (Object.is(prop, -0) || prop < 0) | ||
} |