validup 0.1.10 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/container/module.d.ts +12 -6
- package/dist/container/types.d.ts +20 -10
- package/dist/error/base.d.ts +5 -0
- package/dist/error/check.d.ts +5 -0
- package/dist/error/index.d.ts +2 -0
- package/dist/helpers/error.d.ts +2 -2
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/path-stringify.d.ts +6 -0
- package/dist/index.cjs +204 -94
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +197 -94
- package/dist/index.mjs.map +1 -1
- package/dist/issue/check.d.ts +4 -0
- package/dist/issue/constants.d.ts +4 -0
- package/dist/issue/define.d.ts +5 -0
- package/dist/issue/index.d.ts +4 -0
- package/dist/issue/types.d.ts +47 -0
- package/dist/types.d.ts +2 -15
- package/package.json +5 -2
- package/dist/errors/base.d.ts +0 -2
- package/dist/errors/index.d.ts +0 -3
- package/dist/errors/nested.d.ts +0 -11
- package/dist/errors/validator.d.ts +0 -17
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/helpers/error.ts","../src/constants.ts","../src/helpers/optional-value.ts","../src/errors/base.ts","../src/errors/validator.ts","../src/errors/nested.ts","../src/utils/has-own-property.ts","../src/utils/object.ts","../src/container/check.ts","../src/container/module.ts"],"sourcesContent":["/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { distinctArray } from 'smob';\n\nexport function buildErrorMessageForAttribute(key: string | number) {\n return `The attribute ${String(key)} is invalid.`;\n}\n\nexport function buildErrorMessageForAttributes(input: (number | string)[] | Record<string, any>) {\n let names: string[];\n if (Array.isArray(input)) {\n names = distinctArray(input);\n } else {\n names = Object.keys(input);\n }\n\n if (names.length === 0) {\n return 'An unexpected error occurred.';\n }\n\n if (names.length > 1) {\n return `The attributes ${names.join(', ')} are invalid.`;\n }\n\n return buildErrorMessageForAttribute(names[0]);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum GroupKey {\n WILDCARD = '*',\n}\n\nexport enum OptionalValue {\n /**\n * value: undefined\n */\n UNDEFINED = 'undefined',\n /**\n * value: null & undefined\n */\n NULL = 'null',\n /**\n * value: empty string, false, 0\n */\n FALSY = 'falsy',\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { OptionalValue } from '../constants';\n\n/**\n * Check if input value (type of) satisfies optional value type constraint.\n *\n * @param value\n * @param optionalValue\n */\nexport function isOptionalValue(\n value: unknown,\n optionalValue?: `${OptionalValue}`,\n) {\n const optionValueNormalized = optionalValue || OptionalValue.UNDEFINED;\n\n if (optionValueNormalized === OptionalValue.NULL) {\n return value === null || typeof value === 'undefined';\n }\n\n if (optionValueNormalized === OptionalValue.UNDEFINED) {\n return typeof value === 'undefined';\n }\n\n return !value;\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport class ValidupError extends Error {\n\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { ValidupError } from './base';\n\nexport type ValidupValidatorErrorOptions = {\n path: string,\n pathAbsolute?: string,\n message?: string,\n code?: string | null,\n received?: unknown,\n expected?: unknown\n};\n\nexport class ValidupValidatorError extends ValidupError {\n readonly path: string;\n\n readonly pathAbsolute : string;\n\n readonly code?: string | null;\n\n readonly received?: unknown;\n\n readonly expected?: unknown;\n\n constructor(options: ValidupValidatorErrorOptions) {\n super(options.message);\n\n this.path = options.path;\n this.pathAbsolute = options.pathAbsolute ?? options.path;\n this.code = options.code;\n this.received = options.received;\n this.expected = options.expected;\n }\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { ValidupValidatorError } from './validator';\nimport { ValidupError } from './base';\n\nexport type ValidupNestedErrorOptions = {\n children?: ValidupValidatorError[],\n message?: string\n};\n\nexport class ValidupNestedError extends ValidupError {\n readonly children: ValidupValidatorError[];\n\n constructor(options: ValidupNestedErrorOptions = {}) {\n super(options.message || 'One or many attributes may be invalid.');\n\n this.children = options.children || [];\n }\n\n addChild(child: ValidupValidatorError) {\n this.children.push(child);\n }\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isObject(input: unknown) : input is Record<string, any> {\n return !!input &&\n typeof input === 'object' &&\n !Array.isArray(input);\n}\n","/*\n * Copyright (c) 2025.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from '../utils';\nimport type { IContainer } from './types';\n\nexport function isContainer(input: unknown) : input is IContainer {\n return isObject(input) &&\n typeof input.run === 'function';\n}\n","/*\n * Copyright (c) 2024-2025.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { expandPath, getPathValue, setPathValue } from 'pathtrace';\nimport { GroupKey } from '../constants';\nimport { ValidupNestedError, ValidupValidatorError } from '../errors';\nimport { buildErrorMessageForAttributes, isOptionalValue } from '../helpers';\nimport type {\n ObjectPropertyPath,\n ObjectPropertyPathExtended,\n Validator,\n} from '../types';\nimport { hasOwnProperty, isObject } from '../utils';\nimport { isContainer } from './check';\nimport type {\n ContainerItem, ContainerMountOptions, ContainerOptions, ContainerRunOptions, IContainer,\n} from './types';\n\nexport class Container<\n T extends Record<string, any> = Record<string, any>,\n> implements IContainer {\n protected options : ContainerOptions<T>;\n\n protected items : ContainerItem[];\n\n // ----------------------------------------------\n\n constructor(options: ContainerOptions<T> = {}) {\n this.options = options;\n this.items = [];\n\n this.initialize();\n }\n\n // ----------------------------------------------\n\n mount(container: IContainer) : void;\n\n mount(\n options: ContainerMountOptions,\n container: IContainer\n ): void;\n\n mount(\n key: ObjectPropertyPathExtended<T>,\n data: IContainer | Validator\n ) : void;\n\n mount(\n key: ObjectPropertyPathExtended<T>,\n options: ContainerMountOptions,\n data: IContainer | Validator\n ) : void;\n\n mount(...args: any[]) : void {\n if (args.length < 1) {\n throw new SyntaxError('The mount method requires at least one argument');\n }\n\n let path : string | undefined;\n\n let data: IContainer | Validator | undefined;\n let dataIsContainer : boolean = false;\n\n let options: ContainerMountOptions = {};\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n\n if (typeof arg === 'string') {\n path = arg;\n continue;\n }\n\n if (typeof arg === 'function') {\n data = arg;\n continue;\n }\n\n if (isContainer(arg)) {\n data = arg;\n dataIsContainer = true;\n continue;\n }\n\n if (isObject(arg)) {\n options = args[i];\n }\n }\n\n if (\n !dataIsContainer &&\n (typeof path === 'undefined' || path.length === 0)\n ) {\n throw new SyntaxError('Only a container can be mounted without a key.');\n }\n\n if (typeof data === 'undefined') {\n throw new SyntaxError('No container/validator could be extracted from the fn arguments.');\n }\n\n this.items.push({\n ...options,\n data,\n path,\n });\n }\n\n // ----------------------------------------------\n\n async run(\n data: Record<string, any> = {},\n options: ContainerRunOptions<T> = {},\n ): Promise<T> {\n const output: Record<string, any> = {};\n\n const errors: ValidupValidatorError[] = [];\n const errorKeys : string[] = [];\n\n let pathsToInclude : string[] | undefined;\n if (options.pathsToInclude) {\n pathsToInclude = options.pathsToInclude as string[];\n } else if (this.options.pathsToInclude) {\n pathsToInclude = this.options.pathsToInclude as string[];\n }\n\n let pathsToExclude : string[] | undefined;\n if (options.pathsToExclude) {\n pathsToExclude = options.pathsToExclude as string[];\n } else if (this.options.pathsToInclude) {\n pathsToExclude = this.options.pathsToExclude as string[];\n }\n\n let itemCount = 0;\n for (let i = 0; i < this.items.length; i++) {\n const item = this.items[i];\n\n if (!this.isItemGroupIncluded(item, options.group)) {\n continue;\n }\n\n let pathsCount = 0;\n\n let paths : string[];\n if (item.path) {\n paths = expandPath(data, item.path);\n } else {\n paths = [''];\n }\n\n for (let j = 0; j < paths.length; j++) {\n const path = paths[j];\n const pathAbsolute = this.mergePaths(options.path, paths[j]);\n\n let value : unknown;\n if (hasOwnProperty(output, path)) {\n value = output[path];\n } else if (path.length > 0) {\n value = getPathValue(data, path);\n } else {\n value = data;\n }\n\n if (\n typeof pathsToInclude !== 'undefined' &&\n pathsToInclude.indexOf(path) === -1\n ) {\n continue;\n }\n\n if (\n typeof pathsToExclude !== 'undefined' &&\n pathsToExclude.indexOf(path) !== -1\n ) {\n continue;\n }\n\n try {\n if (\n item.optional &&\n isOptionalValue(value, item.optionalValue)\n ) {\n if (item.optionalInclude) {\n output[path] = value;\n }\n } else if (isContainer(item.data)) {\n const tmp = await item.data.run(\n isObject(value) ? value : {},\n {\n group: options.group,\n flat: true,\n path: pathAbsolute,\n pathsToInclude: options.pathsToInclude,\n // todo: extract defaults for container\n },\n );\n\n const tmpKeys = Object.keys(tmp);\n for (let k = 0; k < tmpKeys.length; k++) {\n output[this.mergePaths(path, tmpKeys[k])] = tmp[tmpKeys[k]];\n }\n } else {\n output[path] = await item.data({\n path,\n pathRaw: item.path ?? '',\n\n pathAbsolute,\n value,\n data,\n group: options.group,\n });\n }\n } catch (e) {\n if (e instanceof ValidupValidatorError) {\n errors.push(e);\n errorKeys.push(pathAbsolute);\n } else if (e instanceof ValidupNestedError) {\n errors.push(...e.children);\n\n if (pathAbsolute.length > 0) {\n errorKeys.push(pathAbsolute);\n } else {\n errorKeys.push(...e.children.map((child) => child.pathAbsolute));\n }\n } else {\n const error = new ValidupValidatorError({\n path,\n pathAbsolute,\n received: value,\n });\n\n if (e instanceof Error) {\n error.cause = e;\n error.message = e.message;\n }\n\n errors.push(error);\n errorKeys.push(pathAbsolute);\n }\n }\n\n pathsCount++;\n }\n\n if (pathsCount > 0) {\n itemCount++;\n }\n }\n\n if (this.options.oneOf) {\n if (errors.length === itemCount) {\n throw new ValidupNestedError({ message: buildErrorMessageForAttributes(errorKeys), children: errors });\n }\n } else if (errors.length > 0) {\n throw new ValidupNestedError({ message: buildErrorMessageForAttributes(errorKeys), children: errors });\n }\n\n if (options.defaults) {\n const defaultKeys = Object.keys(options.defaults);\n for (let i = 0; i < defaultKeys.length; i++) {\n if (\n !hasOwnProperty(output, defaultKeys[i]) ||\n typeof output[defaultKeys[i]] === 'undefined'\n ) {\n output[defaultKeys[i]] = options.defaults[defaultKeys[i] as unknown as ObjectPropertyPath<T>];\n }\n }\n }\n\n if (options.flat) {\n return output as T;\n }\n\n const temp : Record<string, any> = {};\n\n const keys = Object.keys(output);\n for (let i = 0; i < keys.length; i++) {\n setPathValue(temp, keys[i], output[keys[i]]);\n }\n\n return temp as T;\n }\n\n private isItemGroupIncluded(\n item: ContainerItem,\n group?: string,\n ) : boolean {\n if (group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.group) {\n if (Array.isArray(item.group)) {\n if (item.group.indexOf(GroupKey.WILDCARD) !== -1) {\n return true;\n }\n\n if (group && item.group.indexOf(group) !== -1) {\n return true;\n }\n } else {\n if (item.group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.group === group) {\n return true;\n }\n }\n\n return false;\n }\n\n return true;\n }\n\n private mergePaths(...args: (string | undefined)[]) {\n let output : string = '';\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n if (!arg || arg.length === 0) {\n continue;\n }\n\n if (arg.at(0) === '.') {\n output += arg;\n } else {\n output += output.length > 0 ? `.${arg}` : arg;\n }\n }\n\n return output;\n }\n\n protected initialize() : void {\n\n }\n}\n"],"names":["buildErrorMessageForAttribute","key","String","buildErrorMessageForAttributes","input","names","Array","isArray","distinctArray","Object","keys","length","join","GroupKey","OptionalValue","isOptionalValue","value","optionalValue","optionValueNormalized","UNDEFINED","NULL","ValidupError","Error","ValidupValidatorError","options","message","path","pathAbsolute","code","received","expected","ValidupNestedError","addChild","child","children","push","hasOwnProperty","obj","prop","prototype","call","isObject","isContainer","run","Container","mount","args","SyntaxError","data","dataIsContainer","i","arg","items","output","errors","errorKeys","pathsToInclude","pathsToExclude","itemCount","item","isItemGroupIncluded","group","pathsCount","paths","expandPath","j","mergePaths","getPathValue","indexOf","optional","optionalInclude","tmp","flat","tmpKeys","k","pathRaw","e","map","error","cause","oneOf","defaults","defaultKeys","temp","setPathValue","WILDCARD","at","initialize"],"mappings":";;;;;AASO,SAASA,8BAA8BC,GAAoB,EAAA;AAC9D,IAAA,OAAO,CAAC,cAAc,EAAEC,MAAAA,CAAOD,GAAAA,CAAAA,CAAK,YAAY,CAAC;AACrD;AAEO,SAASE,+BAA+BC,KAAgD,EAAA;IAC3F,IAAIC,KAAAA;IACJ,IAAIC,KAAAA,CAAMC,OAAO,CAACH,KAAAA,CAAAA,EAAQ;AACtBC,QAAAA,KAAAA,GAAQG,kBAAAA,CAAcJ,KAAAA,CAAAA;IAC1B,CAAA,MAAO;QACHC,KAAAA,GAAQI,MAAAA,CAAOC,IAAI,CAACN,KAAAA,CAAAA;AACxB,IAAA;IAEA,IAAIC,KAAAA,CAAMM,MAAM,KAAK,CAAA,EAAG;QACpB,OAAO,+BAAA;AACX,IAAA;IAEA,IAAIN,KAAAA,CAAMM,MAAM,GAAG,CAAA,EAAG;QAClB,OAA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MAAAA,IAAUF,GAAAA;YACd,CAAA,MAAO;gBACHE,MAAAA,IAAUA,MAAAA,CAAO1C,MAAM,GAAG,CAAA,GAAI,CAAC,CAAC,EAAEwC,KAAK,GAAGA,GAAAA;AAC9C,YAAA;AACJ,QAAA;QAEA,OAAOE,MAAAA;AACX,IAAA;AAEUkC,IAAAA,UAAAA,GAAoB,CAE9B;;IAtTA,WAAA,CAAY/D,OAAAA,GAA+B,EAAE,CAAE;QAC3C,IAAI,CAACA,OAAO,GAAGA,OAAAA;QACf,IAAI,CAAC4B,KAAK,GAAG,EAAE;AAEf,QAAA,IAAI,CAACmC,UAAU,EAAA;AACnB,IAAA;AAkTJ;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/helpers/error.ts","../src/constants.ts","../src/helpers/optional-value.ts","../src/helpers/path-stringify.ts","../src/error/base.ts","../src/utils/has-own-property.ts","../src/utils/object.ts","../src/issue/check.ts","../src/issue/constants.ts","../src/issue/define.ts","../src/error/check.ts","../src/container/check.ts","../src/container/module.ts"],"sourcesContent":["/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { distinctArray } from 'smob';\n\nexport function buildErrorMessageForAttribute(key: PropertyKey) {\n return `Property ${String(key)} is invalid`;\n}\n\nexport function buildErrorMessageForAttributes(input: PropertyKey[] | Record<string, any>) {\n let names: PropertyKey[];\n if (Array.isArray(input)) {\n names = distinctArray(input);\n } else {\n names = Object.keys(input);\n }\n\n if (names.length === 0) {\n return 'Properties are invalid';\n }\n\n if (names.length > 1) {\n return `Properties ${names.join(', ')} are invalid.`;\n }\n\n return buildErrorMessageForAttribute(names[0]);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum GroupKey {\n WILDCARD = '*',\n}\n\nexport enum OptionalValue {\n /**\n * value: undefined\n */\n UNDEFINED = 'undefined',\n /**\n * value: null & undefined\n */\n NULL = 'null',\n /**\n * value: empty string, false, 0\n */\n FALSY = 'falsy',\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { OptionalValue } from '../constants';\n\n/**\n * Check if input value (type of) satisfies optional value type constraint.\n *\n * @param value\n * @param optionalValue\n */\nexport function isOptionalValue(\n value: unknown,\n optionalValue?: `${OptionalValue}`,\n) {\n const optionValueNormalized = optionalValue || OptionalValue.UNDEFINED;\n\n if (optionValueNormalized === OptionalValue.NULL) {\n return value === null || typeof value === 'undefined';\n }\n\n if (optionValueNormalized === OptionalValue.UNDEFINED) {\n return typeof value === 'undefined';\n }\n\n return !value;\n}\n","/*\n * Copyright (c) 2026.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { arrayToPath } from 'pathtrace';\n\n/**\n * Stringify path array to string.\n *\n * @param input\n */\nexport function stringifyPath(input: PropertyKey[]) : string {\n return arrayToPath(input);\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { buildErrorMessageForAttributes, stringifyPath } from '../helpers';\nimport type { Issue } from '../issue';\n\nexport class ValidupError extends Error {\n readonly issues: Issue[];\n\n constructor(issues: Issue[] = []) {\n const paths = issues.map((issue) => stringifyPath(issue.path));\n super(buildErrorMessageForAttributes(paths));\n\n this.issues = issues;\n }\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isObject(input: unknown) : input is Record<string, any> {\n return !!input &&\n typeof input === 'object' &&\n !Array.isArray(input);\n}\n","/*\n * Copyright (c) 2026.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from '../utils';\nimport type {\n Issue, IssueBase, IssueGroup, IssueItem,\n} from './types';\n\nfunction isIssuePath(input: unknown) : input is PropertyKey[] {\n if (!Array.isArray(input)) {\n return false;\n }\n\n return input.every((el) => typeof el === 'string' ||\n typeof el === 'number' ||\n typeof el === 'symbol');\n}\n\nfunction isBaseIssue(input: unknown) : input is IssueBase & {[ke: string]: any} {\n if (!isObject(input)) {\n return false;\n }\n\n if (typeof input.message !== 'string') {\n return false;\n }\n\n if (!isIssuePath(input.path)) {\n return false;\n }\n\n return typeof input.meta === 'undefined' ||\n isObject(input.meta);\n}\n\nexport function isIssueItem(input: unknown) : input is IssueItem {\n if (!isBaseIssue(input)) {\n return false;\n }\n\n if (input.type !== 'item') {\n return false;\n }\n\n return typeof input.code === 'string';\n}\n\nexport function isIssueGroup(input: unknown) : input is IssueGroup {\n if (!isBaseIssue(input)) {\n return false;\n }\n\n if (input.type !== 'group') {\n return false;\n }\n\n return Array.isArray(input.issues) &&\n input.issues.every((issue) => isIssueItem(issue) || isIssueGroup(issue));\n}\n\nexport function isIssue(input: unknown) : input is Issue {\n return isIssueGroup(input) || isIssueItem(input);\n}\n","/*\n * Copyright (c) 2026.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum IssueCode {\n VALUE_INVALID = 'value_invalid',\n\n ONE_OF_FAILED = 'one_of_failed',\n}\n","/*\n * Copyright (c) 2026.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { IssueCode } from './constants';\nimport type { IssueGroup, IssueItem } from './types';\n\ntype PartialProps<T extends Record<PropertyKey, any>, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & Partial<Pick<T, K>>;\n\nexport function defineIssueItem(data: Omit<PartialProps<IssueItem, 'code'>, 'type'>): IssueItem {\n return {\n type: 'item',\n ...data,\n code: IssueCode.VALUE_INVALID || data.code,\n };\n}\n\nexport function defineIssueGroup(data: Omit<IssueGroup, 'type'>): IssueGroup {\n return {\n type: 'group',\n ...data,\n };\n}\n","/*\n * Copyright (c) 2026.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isIssue } from '../issue';\nimport { hasOwnProperty, isObject } from '../utils';\nimport { ValidupError } from './base';\n\nexport function isError(input: unknown) : input is Error & { [key: string]: any } {\n if (!isObject(input)) {\n return false;\n }\n\n return typeof input.message === 'string';\n}\n\nexport function isValidupError(error: unknown) : error is ValidupError {\n if (!isError(error)) {\n return false;\n }\n\n if (error instanceof ValidupError) {\n return true;\n }\n\n if (!hasOwnProperty(error, 'issues')) {\n return false;\n }\n\n return Array.isArray(error.issues) &&\n error.issues.every((issue) => isIssue(issue));\n}\n","/*\n * Copyright (c) 2025.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from '../utils';\nimport type { IContainer } from './types';\n\nexport function isContainer(input: unknown) : input is IContainer {\n return isObject(input) &&\n typeof input.run === 'function';\n}\n","/*\n * Copyright (c) 2024-2025.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { Path } from 'pathtrace';\nimport {\n expandPath, getPathValue, pathToArray, setPathValue,\n} from 'pathtrace';\nimport { GroupKey } from '../constants';\nimport { ValidupError, isError, isValidupError } from '../error';\nimport { buildErrorMessageForAttribute, isOptionalValue } from '../helpers';\nimport type { Validator } from '../types';\nimport { hasOwnProperty, isObject } from '../utils';\nimport { isContainer } from './check';\nimport type {\n ContainerOptions, ContainerRunOptions, IContainer, Mount, MountOptions,\n} from './types';\nimport type { Issue } from '../issue';\nimport { IssueCode, defineIssueGroup, defineIssueItem } from '../issue';\n\nexport class Container<\n T extends Record<string, any> = Record<string, any>,\n> implements IContainer {\n protected options : ContainerOptions<T>;\n\n protected items : Mount[];\n\n // ----------------------------------------------\n\n constructor(options: ContainerOptions<T> = {}) {\n this.options = options;\n this.items = [];\n\n this.initialize();\n }\n\n // ----------------------------------------------\n\n mount(container: IContainer) : void;\n\n mount(\n options: MountOptions,\n container: IContainer\n ): void;\n\n mount(\n // eslint-disable-next-line @typescript-eslint/ban-types\n key: Path<T> | (string & {}),\n data: IContainer | Validator\n ) : void;\n\n mount(\n // eslint-disable-next-line @typescript-eslint/ban-types\n key: Path<T> | (string & {}),\n options: MountOptions,\n data: IContainer | Validator\n ) : void;\n\n mount(...args: any[]) : void {\n if (args.length < 1) {\n throw new SyntaxError('The mount method requires at least one argument');\n }\n\n let path : string | undefined;\n\n let data: IContainer | Validator | undefined;\n let dataIsContainer : boolean = false;\n\n let options: MountOptions = {};\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n\n if (typeof arg === 'string') {\n path = arg;\n continue;\n }\n\n if (typeof arg === 'function') {\n data = arg;\n continue;\n }\n\n if (isContainer(arg)) {\n data = arg;\n dataIsContainer = true;\n continue;\n }\n\n if (isObject(arg)) {\n options = args[i];\n }\n }\n\n if (\n !dataIsContainer &&\n (typeof path === 'undefined' || path.length === 0)\n ) {\n throw new SyntaxError('Only a container can be mounted without a key.');\n }\n\n if (typeof data === 'undefined') {\n throw new SyntaxError('No container/validator could be extracted from the fn arguments.');\n }\n\n if (isContainer(data)) {\n this.items.push({\n options,\n data,\n path,\n type: 'container',\n });\n\n return;\n }\n\n this.items.push({\n options,\n data,\n path,\n type: 'validator',\n });\n }\n\n // ----------------------------------------------\n\n /**\n * @throws ValidupError\n * @param data\n * @param options\n */\n async run(\n data: Record<string, any> = {},\n options: ContainerRunOptions<T> = {},\n ): Promise<T> {\n let pathsToInclude : string[] | undefined;\n if (options.pathsToInclude) {\n pathsToInclude = options.pathsToInclude as string[];\n } else if (this.options.pathsToInclude) {\n pathsToInclude = this.options.pathsToInclude as string[];\n }\n\n let pathsToExclude : string[] | undefined;\n if (options.pathsToExclude) {\n pathsToExclude = options.pathsToExclude as string[];\n } else if (this.options.pathsToInclude) {\n pathsToExclude = this.options.pathsToExclude as string[];\n }\n\n const output: Record<string, any> = {};\n\n const issues : Issue[] = [];\n\n let itemCount : number = 0;\n let errorCount : number = 0;\n\n for (let i = 0; i < this.items.length; i++) {\n const item = this.items[i];\n\n if (!this.isItemGroupIncluded(item, options.group)) {\n // todo: maybe add issue info\n continue;\n }\n\n let pathCount = 0;\n let pathFailed = false;\n\n let keys : string[];\n if (item.path) {\n keys = expandPath(data, item.path);\n } else {\n keys = [''];\n }\n\n for (let j = 0; j < keys.length; j++) {\n const key = keys[j];\n const keyParts = key ? pathToArray(key) : [];\n\n const pathRelative = keyParts.at(-1);\n const pathAbsolute = [\n ...(options.path ? options.path : []),\n ...keyParts,\n ];\n\n let value : unknown;\n if (key.length > 0) {\n value = hasOwnProperty(output, key) ?\n output[key] :\n getPathValue(data, key);\n } else {\n value = data;\n }\n\n if (\n typeof pathsToInclude !== 'undefined' &&\n pathsToInclude.indexOf(key) === -1\n ) {\n // todo: maybe add issue info\n continue;\n }\n\n if (\n typeof pathsToExclude !== 'undefined' &&\n pathsToExclude.indexOf(key) !== -1\n ) {\n // todo: maybe add issue info\n continue;\n }\n\n try {\n if (\n item.options.optional &&\n isOptionalValue(value, item.options.optionalValue)\n ) {\n if (item.options.optionalInclude) {\n output[key] = value;\n }\n } else if (item.type === 'container') {\n const tmp = await item.data.run(\n isObject(value) ? value : {},\n {\n group: options.group,\n flat: true,\n path: pathAbsolute,\n pathsToInclude: options.pathsToInclude,\n // todo: extract defaults for container\n },\n );\n\n const tmpKeys = Object.keys(tmp);\n for (let k = 0; k < tmpKeys.length; k++) {\n output[this.mergePaths(key, tmpKeys[k])] = tmp[tmpKeys[k]];\n }\n } else if (item.type === 'validator') {\n output[key] = await item.data({\n key,\n path: pathAbsolute,\n\n value,\n data,\n group: options.group,\n });\n }\n } catch (e) {\n const childIssues : Issue[] = [];\n\n if (isValidupError(e)) {\n for (let i = 0; i < e.issues.length; i++) {\n const issue = e.issues[i];\n\n childIssues.push({\n ...issue,\n path: [\n ...keyParts,\n ...(issue.path || []),\n ],\n });\n }\n } else if (isError(e)) {\n childIssues.push(defineIssueItem({\n path: keyParts,\n message: e.message,\n }));\n }\n\n if (pathRelative) {\n if (item.type === 'container' || childIssues.length > 1) {\n const group = defineIssueGroup({\n message: buildErrorMessageForAttribute(pathRelative),\n path: keyParts,\n issues: childIssues,\n });\n\n issues.push(group);\n } else {\n issues.push(...childIssues);\n }\n } else {\n issues.push(...childIssues);\n }\n\n pathFailed = true;\n }\n\n pathCount++;\n }\n\n if (pathCount > 0) {\n itemCount++;\n\n if (pathFailed) {\n errorCount++;\n }\n }\n }\n\n if (this.options.oneOf) {\n if (errorCount === itemCount) {\n const group = defineIssueGroup({\n code: IssueCode.ONE_OF_FAILED,\n message: 'None of the branches succeeded',\n issues,\n path: options.path ? options.path : [],\n });\n\n throw new ValidupError([group]);\n }\n } else if (errorCount > 0) {\n throw new ValidupError(issues);\n }\n\n if (options.defaults) {\n const defaultKeys = Object.keys(options.defaults);\n for (let i = 0; i < defaultKeys.length; i++) {\n if (\n !hasOwnProperty(output, defaultKeys[i]) ||\n typeof output[defaultKeys[i]] === 'undefined'\n ) {\n output[defaultKeys[i]] = options.defaults[defaultKeys[i] as unknown as Path<T>];\n }\n }\n }\n\n if (options.flat) {\n return output as T;\n }\n\n const temp : Record<string, any> = {};\n\n const keys = Object.keys(output);\n for (let i = 0; i < keys.length; i++) {\n setPathValue(temp, keys[i], output[keys[i]]);\n }\n\n return temp as T;\n }\n\n private isItemGroupIncluded(\n item: Mount,\n group?: string,\n ) : boolean {\n if (group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.options.group) {\n if (Array.isArray(item.options.group)) {\n if (item.options.group.indexOf(GroupKey.WILDCARD) !== -1) {\n return true;\n }\n\n if (group && item.options.group.indexOf(group) !== -1) {\n return true;\n }\n } else {\n if (item.options.group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.options.group === group) {\n return true;\n }\n }\n\n return false;\n }\n\n return true;\n }\n\n private mergePaths(...args: (string | undefined)[]) {\n let output : string = '';\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n if (!arg || arg.length === 0) {\n continue;\n }\n\n if (arg.at(0) === '.') {\n output += arg;\n } else {\n output += output.length > 0 ? `.${arg}` : arg;\n }\n }\n\n return output;\n }\n\n protected initialize() : void {\n\n }\n}\n"],"names":["buildErrorMessageForAttribute","key","String","buildErrorMessageForAttributes","input","names","Array","isArray","distinctArray","Object","keys","length","join","GroupKey","OptionalValue","isOptionalValue","value","optionalValue","optionValueNormalized","UNDEFINED","NULL","stringifyPath","arrayToPath","ValidupError","Error","issues","paths","map","issue","path","hasOwnProperty","obj","prop","prototype","call","isObject","isIssuePath","every","el","isBaseIssue","message","meta","isIssueItem","type","code","isIssueGroup","isIssue","IssueCode","defineIssueItem","data","VALUE_INVALID","defineIssueGroup","isError","isValidupError","error","isContainer","run","Container","mount","args","SyntaxError","dataIsContainer","options","i","arg","items","push","pathsToInclude","pathsToExclude","output","itemCount","errorCount","item","isItemGroupIncluded","group","pathCount","pathFailed","expandPath","j","keyParts","pathToArray","pathRelative","at","pathAbsolute","getPathValue","indexOf","optional","optionalInclude","tmp","flat","tmpKeys","k","mergePaths","e","childIssues","oneOf","ONE_OF_FAILED","defaults","defaultKeys","temp","setPathValue","WILDCARD","initialize"],"mappings":";;;;;AASO,SAASA,8BAA8BC,GAAgB,EAAA;AAC1D,IAAA,OAAO,CAAC,SAAS,EAAEC,MAAAA,CAAOD,GAAAA,CA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A,WAAA,CAAYxC,OAAAA,GAA+B,EAAE,CAAE;QAC3C,IAAI,CAACA,OAAO,GAAGA,OAAAA;QACf,IAAI,CAACG,KAAK,GAAG,EAAE;AAEf,QAAA,IAAI,CAACqC,UAAU,EAAA;AACnB,IAAA;AAsWJ;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { distinctArray } from 'smob';
|
|
2
|
-
import { expandPath, getPathValue, setPathValue } from 'pathtrace';
|
|
2
|
+
import { arrayToPath, expandPath, pathToArray, getPathValue, setPathValue } from 'pathtrace';
|
|
3
3
|
|
|
4
4
|
function buildErrorMessageForAttribute(key) {
|
|
5
|
-
return `
|
|
5
|
+
return `Property ${String(key)} is invalid`;
|
|
6
6
|
}
|
|
7
7
|
function buildErrorMessageForAttributes(input) {
|
|
8
8
|
let names;
|
|
@@ -12,10 +12,10 @@ function buildErrorMessageForAttributes(input) {
|
|
|
12
12
|
names = Object.keys(input);
|
|
13
13
|
}
|
|
14
14
|
if (names.length === 0) {
|
|
15
|
-
return '
|
|
15
|
+
return 'Properties are invalid';
|
|
16
16
|
}
|
|
17
17
|
if (names.length > 1) {
|
|
18
|
-
return `
|
|
18
|
+
return `Properties ${names.join(', ')} are invalid.`;
|
|
19
19
|
}
|
|
20
20
|
return buildErrorMessageForAttribute(names[0]);
|
|
21
21
|
}
|
|
@@ -58,32 +58,19 @@ var OptionalValue = /*#__PURE__*/ function(OptionalValue) {
|
|
|
58
58
|
return !value;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
class ValidupValidatorError extends ValidupError {
|
|
70
|
-
constructor(options){
|
|
71
|
-
super(options.message);
|
|
72
|
-
this.path = options.path;
|
|
73
|
-
this.pathAbsolute = options.pathAbsolute ?? options.path;
|
|
74
|
-
this.code = options.code;
|
|
75
|
-
this.received = options.received;
|
|
76
|
-
this.expected = options.expected;
|
|
77
|
-
}
|
|
61
|
+
/**
|
|
62
|
+
* Stringify path array to string.
|
|
63
|
+
*
|
|
64
|
+
* @param input
|
|
65
|
+
*/ function stringifyPath(input) {
|
|
66
|
+
return arrayToPath(input);
|
|
78
67
|
}
|
|
79
68
|
|
|
80
|
-
class
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
super(options.message || 'One or many attributes may be invalid.');
|
|
86
|
-
this.children = options.children || [];
|
|
69
|
+
class ValidupError extends Error {
|
|
70
|
+
constructor(issues = []){
|
|
71
|
+
const paths = issues.map((issue)=>stringifyPath(issue.path));
|
|
72
|
+
super(buildErrorMessageForAttributes(paths));
|
|
73
|
+
this.issues = issues;
|
|
87
74
|
}
|
|
88
75
|
}
|
|
89
76
|
|
|
@@ -106,6 +93,90 @@ function hasOwnProperty(obj, prop) {
|
|
|
106
93
|
return !!input && typeof input === 'object' && !Array.isArray(input);
|
|
107
94
|
}
|
|
108
95
|
|
|
96
|
+
function isIssuePath(input) {
|
|
97
|
+
if (!Array.isArray(input)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return input.every((el)=>typeof el === 'string' || typeof el === 'number' || typeof el === 'symbol');
|
|
101
|
+
}
|
|
102
|
+
function isBaseIssue(input) {
|
|
103
|
+
if (!isObject(input)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if (typeof input.message !== 'string') {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
if (!isIssuePath(input.path)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
return typeof input.meta === 'undefined' || isObject(input.meta);
|
|
113
|
+
}
|
|
114
|
+
function isIssueItem(input) {
|
|
115
|
+
if (!isBaseIssue(input)) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (input.type !== 'item') {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
return typeof input.code === 'string';
|
|
122
|
+
}
|
|
123
|
+
function isIssueGroup(input) {
|
|
124
|
+
if (!isBaseIssue(input)) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
if (input.type !== 'group') {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return Array.isArray(input.issues) && input.issues.every((issue)=>isIssueItem(issue) || isIssueGroup(issue));
|
|
131
|
+
}
|
|
132
|
+
function isIssue(input) {
|
|
133
|
+
return isIssueGroup(input) || isIssueItem(input);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/*
|
|
137
|
+
* Copyright (c) 2026.
|
|
138
|
+
* Author Peter Placzek (tada5hi)
|
|
139
|
+
* For the full copyright and license information,
|
|
140
|
+
* view the LICENSE file that was distributed with this source code.
|
|
141
|
+
*/ var IssueCode = /*#__PURE__*/ function(IssueCode) {
|
|
142
|
+
IssueCode["VALUE_INVALID"] = "value_invalid";
|
|
143
|
+
IssueCode["ONE_OF_FAILED"] = "one_of_failed";
|
|
144
|
+
return IssueCode;
|
|
145
|
+
}({});
|
|
146
|
+
|
|
147
|
+
function defineIssueItem(data) {
|
|
148
|
+
return {
|
|
149
|
+
type: 'item',
|
|
150
|
+
...data,
|
|
151
|
+
code: IssueCode.VALUE_INVALID || data.code
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function defineIssueGroup(data) {
|
|
155
|
+
return {
|
|
156
|
+
type: 'group',
|
|
157
|
+
...data
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isError(input) {
|
|
162
|
+
if (!isObject(input)) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return typeof input.message === 'string';
|
|
166
|
+
}
|
|
167
|
+
function isValidupError(error) {
|
|
168
|
+
if (!isError(error)) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
if (error instanceof ValidupError) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
if (!hasOwnProperty(error, 'issues')) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return Array.isArray(error.issues) && error.issues.every((issue)=>isIssue(issue));
|
|
178
|
+
}
|
|
179
|
+
|
|
109
180
|
function isContainer(input) {
|
|
110
181
|
return isObject(input) && typeof input.run === 'function';
|
|
111
182
|
}
|
|
@@ -144,17 +215,28 @@ class Container {
|
|
|
144
215
|
if (typeof data === 'undefined') {
|
|
145
216
|
throw new SyntaxError('No container/validator could be extracted from the fn arguments.');
|
|
146
217
|
}
|
|
218
|
+
if (isContainer(data)) {
|
|
219
|
+
this.items.push({
|
|
220
|
+
options,
|
|
221
|
+
data,
|
|
222
|
+
path,
|
|
223
|
+
type: 'container'
|
|
224
|
+
});
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
147
227
|
this.items.push({
|
|
148
|
-
|
|
228
|
+
options,
|
|
149
229
|
data,
|
|
150
|
-
path
|
|
230
|
+
path,
|
|
231
|
+
type: 'validator'
|
|
151
232
|
});
|
|
152
233
|
}
|
|
153
234
|
// ----------------------------------------------
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
235
|
+
/**
|
|
236
|
+
* @throws ValidupError
|
|
237
|
+
* @param data
|
|
238
|
+
* @param options
|
|
239
|
+
*/ async run(data = {}, options = {}) {
|
|
158
240
|
let pathsToInclude;
|
|
159
241
|
if (options.pathsToInclude) {
|
|
160
242
|
pathsToInclude = options.pathsToInclude;
|
|
@@ -167,44 +249,51 @@ class Container {
|
|
|
167
249
|
} else if (this.options.pathsToInclude) {
|
|
168
250
|
pathsToExclude = this.options.pathsToExclude;
|
|
169
251
|
}
|
|
252
|
+
const output = {};
|
|
253
|
+
const issues = [];
|
|
170
254
|
let itemCount = 0;
|
|
255
|
+
let errorCount = 0;
|
|
171
256
|
for(let i = 0; i < this.items.length; i++){
|
|
172
257
|
const item = this.items[i];
|
|
173
258
|
if (!this.isItemGroupIncluded(item, options.group)) {
|
|
174
259
|
continue;
|
|
175
260
|
}
|
|
176
|
-
let
|
|
177
|
-
let
|
|
261
|
+
let pathCount = 0;
|
|
262
|
+
let pathFailed = false;
|
|
263
|
+
let keys;
|
|
178
264
|
if (item.path) {
|
|
179
|
-
|
|
265
|
+
keys = expandPath(data, item.path);
|
|
180
266
|
} else {
|
|
181
|
-
|
|
267
|
+
keys = [
|
|
182
268
|
''
|
|
183
269
|
];
|
|
184
270
|
}
|
|
185
|
-
for(let j = 0; j <
|
|
186
|
-
const
|
|
187
|
-
const
|
|
271
|
+
for(let j = 0; j < keys.length; j++){
|
|
272
|
+
const key = keys[j];
|
|
273
|
+
const keyParts = key ? pathToArray(key) : [];
|
|
274
|
+
const pathRelative = keyParts.at(-1);
|
|
275
|
+
const pathAbsolute = [
|
|
276
|
+
...options.path ? options.path : [],
|
|
277
|
+
...keyParts
|
|
278
|
+
];
|
|
188
279
|
let value;
|
|
189
|
-
if (
|
|
190
|
-
value = output[
|
|
191
|
-
} else if (path.length > 0) {
|
|
192
|
-
value = getPathValue(data, path);
|
|
280
|
+
if (key.length > 0) {
|
|
281
|
+
value = hasOwnProperty(output, key) ? output[key] : getPathValue(data, key);
|
|
193
282
|
} else {
|
|
194
283
|
value = data;
|
|
195
284
|
}
|
|
196
|
-
if (typeof pathsToInclude !== 'undefined' && pathsToInclude.indexOf(
|
|
285
|
+
if (typeof pathsToInclude !== 'undefined' && pathsToInclude.indexOf(key) === -1) {
|
|
197
286
|
continue;
|
|
198
287
|
}
|
|
199
|
-
if (typeof pathsToExclude !== 'undefined' && pathsToExclude.indexOf(
|
|
288
|
+
if (typeof pathsToExclude !== 'undefined' && pathsToExclude.indexOf(key) !== -1) {
|
|
200
289
|
continue;
|
|
201
290
|
}
|
|
202
291
|
try {
|
|
203
|
-
if (item.optional && isOptionalValue(value, item.optionalValue)) {
|
|
204
|
-
if (item.optionalInclude) {
|
|
205
|
-
output[
|
|
292
|
+
if (item.options.optional && isOptionalValue(value, item.options.optionalValue)) {
|
|
293
|
+
if (item.options.optionalInclude) {
|
|
294
|
+
output[key] = value;
|
|
206
295
|
}
|
|
207
|
-
} else if (
|
|
296
|
+
} else if (item.type === 'container') {
|
|
208
297
|
const tmp = await item.data.run(isObject(value) ? value : {}, {
|
|
209
298
|
group: options.group,
|
|
210
299
|
flat: true,
|
|
@@ -213,61 +302,75 @@ class Container {
|
|
|
213
302
|
});
|
|
214
303
|
const tmpKeys = Object.keys(tmp);
|
|
215
304
|
for(let k = 0; k < tmpKeys.length; k++){
|
|
216
|
-
output[this.mergePaths(
|
|
305
|
+
output[this.mergePaths(key, tmpKeys[k])] = tmp[tmpKeys[k]];
|
|
217
306
|
}
|
|
218
|
-
} else {
|
|
219
|
-
output[
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
pathAbsolute,
|
|
307
|
+
} else if (item.type === 'validator') {
|
|
308
|
+
output[key] = await item.data({
|
|
309
|
+
key,
|
|
310
|
+
path: pathAbsolute,
|
|
223
311
|
value,
|
|
224
312
|
data,
|
|
225
313
|
group: options.group
|
|
226
314
|
});
|
|
227
315
|
}
|
|
228
316
|
} catch (e) {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
317
|
+
const childIssues = [];
|
|
318
|
+
if (isValidupError(e)) {
|
|
319
|
+
for(let i = 0; i < e.issues.length; i++){
|
|
320
|
+
const issue = e.issues[i];
|
|
321
|
+
childIssues.push({
|
|
322
|
+
...issue,
|
|
323
|
+
path: [
|
|
324
|
+
...keyParts,
|
|
325
|
+
...issue.path || []
|
|
326
|
+
]
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
} else if (isError(e)) {
|
|
330
|
+
childIssues.push(defineIssueItem({
|
|
331
|
+
path: keyParts,
|
|
332
|
+
message: e.message
|
|
333
|
+
}));
|
|
334
|
+
}
|
|
335
|
+
if (pathRelative) {
|
|
336
|
+
if (item.type === 'container' || childIssues.length > 1) {
|
|
337
|
+
const group = defineIssueGroup({
|
|
338
|
+
message: buildErrorMessageForAttribute(pathRelative),
|
|
339
|
+
path: keyParts,
|
|
340
|
+
issues: childIssues
|
|
341
|
+
});
|
|
342
|
+
issues.push(group);
|
|
236
343
|
} else {
|
|
237
|
-
|
|
344
|
+
issues.push(...childIssues);
|
|
238
345
|
}
|
|
239
346
|
} else {
|
|
240
|
-
|
|
241
|
-
path,
|
|
242
|
-
pathAbsolute,
|
|
243
|
-
received: value
|
|
244
|
-
});
|
|
245
|
-
if (e instanceof Error) {
|
|
246
|
-
error.cause = e;
|
|
247
|
-
error.message = e.message;
|
|
248
|
-
}
|
|
249
|
-
errors.push(error);
|
|
250
|
-
errorKeys.push(pathAbsolute);
|
|
347
|
+
issues.push(...childIssues);
|
|
251
348
|
}
|
|
349
|
+
pathFailed = true;
|
|
252
350
|
}
|
|
253
|
-
|
|
351
|
+
pathCount++;
|
|
254
352
|
}
|
|
255
|
-
if (
|
|
353
|
+
if (pathCount > 0) {
|
|
256
354
|
itemCount++;
|
|
355
|
+
if (pathFailed) {
|
|
356
|
+
errorCount++;
|
|
357
|
+
}
|
|
257
358
|
}
|
|
258
359
|
}
|
|
259
360
|
if (this.options.oneOf) {
|
|
260
|
-
if (
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
361
|
+
if (errorCount === itemCount) {
|
|
362
|
+
const group = defineIssueGroup({
|
|
363
|
+
code: IssueCode.ONE_OF_FAILED,
|
|
364
|
+
message: 'None of the branches succeeded',
|
|
365
|
+
issues,
|
|
366
|
+
path: options.path ? options.path : []
|
|
264
367
|
});
|
|
368
|
+
throw new ValidupError([
|
|
369
|
+
group
|
|
370
|
+
]);
|
|
265
371
|
}
|
|
266
|
-
} else if (
|
|
267
|
-
throw new
|
|
268
|
-
message: buildErrorMessageForAttributes(errorKeys),
|
|
269
|
-
children: errors
|
|
270
|
-
});
|
|
372
|
+
} else if (errorCount > 0) {
|
|
373
|
+
throw new ValidupError(issues);
|
|
271
374
|
}
|
|
272
375
|
if (options.defaults) {
|
|
273
376
|
const defaultKeys = Object.keys(options.defaults);
|
|
@@ -291,19 +394,19 @@ class Container {
|
|
|
291
394
|
if (group === GroupKey.WILDCARD) {
|
|
292
395
|
return true;
|
|
293
396
|
}
|
|
294
|
-
if (item.group) {
|
|
295
|
-
if (Array.isArray(item.group)) {
|
|
296
|
-
if (item.group.indexOf(GroupKey.WILDCARD) !== -1) {
|
|
397
|
+
if (item.options.group) {
|
|
398
|
+
if (Array.isArray(item.options.group)) {
|
|
399
|
+
if (item.options.group.indexOf(GroupKey.WILDCARD) !== -1) {
|
|
297
400
|
return true;
|
|
298
401
|
}
|
|
299
|
-
if (group && item.group.indexOf(group) !== -1) {
|
|
402
|
+
if (group && item.options.group.indexOf(group) !== -1) {
|
|
300
403
|
return true;
|
|
301
404
|
}
|
|
302
405
|
} else {
|
|
303
|
-
if (item.group === GroupKey.WILDCARD) {
|
|
406
|
+
if (item.options.group === GroupKey.WILDCARD) {
|
|
304
407
|
return true;
|
|
305
408
|
}
|
|
306
|
-
if (item.group === group) {
|
|
409
|
+
if (item.options.group === group) {
|
|
307
410
|
return true;
|
|
308
411
|
}
|
|
309
412
|
}
|
|
@@ -335,5 +438,5 @@ class Container {
|
|
|
335
438
|
}
|
|
336
439
|
}
|
|
337
440
|
|
|
338
|
-
export { Container, GroupKey, OptionalValue, ValidupError,
|
|
441
|
+
export { Container, GroupKey, IssueCode, OptionalValue, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, defineIssueGroup, defineIssueItem, hasOwnProperty, isContainer, isError, isIssue, isIssueGroup, isIssueItem, isObject, isOptionalValue, isValidupError, stringifyPath };
|
|
339
442
|
//# sourceMappingURL=index.mjs.map
|