@validup/adapter-validator
Advanced tools
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.cjs","sources":["../src/chain.ts","../src/error.ts","../src/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 { type ValidationChain, body } from 'express-validator';\n\nexport function createValidationChain() : ValidationChain {\n return body();\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 type { ValidationError } from 'express-validator/lib/base';\nimport { ValidupNestedError, ValidupValidatorError, buildErrorMessageForAttributes } from 'validup';\n\ntype ErrorOptions = {\n path: string,\n pathAbsolute?: string\n};\n\nfunction generateAttributeErrors(\n error: ValidationError,\n options: ErrorOptions,\n) : ValidupValidatorError[] {\n const output : ValidupValidatorError[] = [];\n switch (error.type) {\n case 'field': {\n const name = error.path || options.path;\n const message = error.msg || buildErrorMessageForAttributes([name]);\n\n output.push(new ValidupValidatorError({\n path: name,\n pathAbsolute: options.pathAbsolute || options.path,\n received: error.value,\n message,\n }));\n break;\n }\n case 'alternative': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i], options));\n }\n break;\n }\n case 'alternative_grouped': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n for (let j = 0; j < error.nestedErrors[i].length; j++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i][j], options));\n }\n }\n }\n }\n\n return output;\n}\n\nexport function buildNestedError(\n errors: ValidationError[],\n options: ErrorOptions,\n): ValidupNestedError {\n const base = new ValidupNestedError();\n const names : (number | string)[] = [];\n for (let i = 0; i < errors.length; i++) {\n const children = generateAttributeErrors(errors[i], options);\n for (let j = 0; j < children.length; j++) {\n base.addChild(children[j]);\n names.push(children[j].path);\n }\n }\n\n base.message = buildErrorMessageForAttributes(names);\n\n return base;\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 type { ContextRunner, FieldValidationError } from 'express-validator';\nimport { distinctArray } from 'smob';\nimport type { Validator, ValidatorContext } from 'validup';\nimport { ValidupError } from 'validup';\nimport { buildNestedError } from './error';\n\ntype ContextRunnerCreateFn = (\n ctx: ValidatorContext\n) => ContextRunner;\n\nexport function createValidator(\n input: ContextRunnerCreateFn | ContextRunner,\n) : Validator {\n return async (ctx): Promise<unknown> => {\n let runner : ContextRunner;\n if (typeof input === 'function') {\n runner = input(ctx);\n } else {\n runner = input;\n }\n\n const outcome = await runner.run({\n body: ctx.value,\n });\n\n const [field] = outcome.context.getData({ requiredOnly: false });\n if (field) {\n const errors = distinctArray(outcome.context.errors.filter(\n (error) => error.type === 'field' &&\n error.location === field.location &&\n error.path === field.path,\n ) as FieldValidationError[]);\n\n if (errors.length > 0) {\n throw buildNestedError(errors, {\n path: ctx.path,\n pathAbsolute: ctx.pathAbsolute,\n });\n }\n\n return field.value;\n }\n\n throw new ValidupError(`The attribute ${ctx.path} could not be validated.`);\n };\n}\n"],"names":["createValidationChain","body","generateAttributeErrors","error","options","output","type","name","path","message","msg","buildErrorMessageForAttributes","push","ValidupValidatorError","pathAbsolute","received","value","i","nestedErrors","length","j","buildNestedError","errors","base","ValidupNestedError","names","children","addChild","createValidator","input","ctx","runner","outcome","run","field","context","getData","requiredOnly","distinctArray","filter","location","ValidupError"],"mappings":";;;;;;AASO,SAASA,qBAAAA,GAAAA;IACZ,OAAOC,qBAAAA,EAAAA;AACX;;ACIA,SAASC,uBAAAA,CACLC,KAAsB,EACtBC,OAAqB,EAAA;AAErB,IAAA,MAAMC,SAAmC,EAAE;AAC3C,IAAA,OAAQF,MAAMG,IAAI;QACd,KAAK,OAAA;AAAS,YAAA;AACV,gBAAA,MAAMC,IAAOJ,GAAAA,KAAAA,CAAMK,IAAI,IAAIJ,QAAQI,IAAI;AACvC,gB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OAAA,IACtBH,KAAMqC,CAAAA,QAAQ,KAAKN,KAAAA,CAAMM,QAAQ,IACjCrC,KAAMK,CAAAA,IAAI,KAAK0B,KAAAA,CAAM1B,IAAI,CAAA,CAAA;YAGjC,IAAIc,MAAAA,CAAOH,MAAM,GAAG,CAAG,EAAA;AACnB,gBAAA,MAAME,iBAAiBC,MAAQ,EAAA;AAC3Bd,oBAAAA,IAAAA,EAAMsB,IAAItB,IAAI;AACdM,oBAAAA,YAAAA,EAAcgB,IAAIhB;AACtB,iBAAA,CAAA;AACJ;AAEA,YAAA,OAAOoB,MAAMlB,KAAK;AACtB;QAEA,MAAM,IAAIyB,qBAAa,CAAC,cAAc,EAAEX,GAAItB,CAAAA,IAAI,CAAC,wBAAwB,CAAC,CAAA;AAC9E,KAAA;AACJ;;;;;"} | ||
| {"version":3,"file":"index.cjs","sources":["../src/chain.ts","../src/error.ts","../src/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 { type ValidationChain, body } from 'express-validator';\n\nexport function createValidationChain() : ValidationChain {\n return body();\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 type { ValidationError } from 'express-validator/lib/base';\nimport { ValidupNestedError, ValidupValidatorError, buildErrorMessageForAttributes } from 'validup';\n\ntype ErrorOptions = {\n path: string,\n pathAbsolute?: string\n};\n\nfunction generateAttributeErrors(\n error: ValidationError,\n options: ErrorOptions,\n) : ValidupValidatorError[] {\n const output : ValidupValidatorError[] = [];\n switch (error.type) {\n case 'field': {\n const name = error.path || options.path;\n const message = error.msg || buildErrorMessageForAttributes([name]);\n\n output.push(new ValidupValidatorError({\n path: name,\n pathAbsolute: options.pathAbsolute || options.path,\n received: error.value,\n message,\n }));\n break;\n }\n case 'alternative': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i], options));\n }\n break;\n }\n case 'alternative_grouped': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n for (let j = 0; j < error.nestedErrors[i].length; j++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i][j], options));\n }\n }\n }\n }\n\n return output;\n}\n\nexport function buildNestedError(\n errors: ValidationError[],\n options: ErrorOptions,\n): ValidupNestedError {\n const base = new ValidupNestedError();\n const names : (number | string)[] = [];\n for (let i = 0; i < errors.length; i++) {\n const children = generateAttributeErrors(errors[i], options);\n for (let j = 0; j < children.length; j++) {\n base.addChild(children[j]);\n names.push(children[j].path);\n }\n }\n\n base.message = buildErrorMessageForAttributes(names);\n\n return base;\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 type { ContextRunner, FieldValidationError } from 'express-validator';\nimport { distinctArray } from 'smob';\nimport type { Validator, ValidatorContext } from 'validup';\nimport { ValidupError } from 'validup';\nimport { buildNestedError } from './error';\n\ntype ContextRunnerCreateFn = (\n ctx: ValidatorContext\n) => ContextRunner;\n\nexport function createValidator(\n input: ContextRunnerCreateFn | ContextRunner,\n) : Validator {\n return async (ctx): Promise<unknown> => {\n let runner : ContextRunner;\n if (typeof input === 'function') {\n runner = input(ctx);\n } else {\n runner = input;\n }\n\n const outcome = await runner.run({\n body: ctx.value,\n });\n\n const [field] = outcome.context.getData({ requiredOnly: false });\n if (field) {\n const errors = distinctArray(outcome.context.errors.filter(\n (error) => error.type === 'field' &&\n error.location === field.location &&\n error.path === field.path,\n ) as FieldValidationError[]);\n\n if (errors.length > 0) {\n throw buildNestedError(errors, {\n path: ctx.path,\n pathAbsolute: ctx.pathAbsolute,\n });\n }\n\n return field.value;\n }\n\n throw new ValidupError(`The attribute ${ctx.path} could not be validated.`);\n };\n}\n"],"names":["createValidationChain","body","generateAttributeErrors","error","options","output","type","name","path","message","msg","buildErrorMessageForAttributes","push","ValidupValidatorError","pathAbsolute","received","value","i","nestedErrors","length","j","buildNestedError","errors","base","ValidupNestedError","names","children","addChild","createValidator","input","ctx","runner","outcome","run","field","context","getData","requiredOnly","distinctArray","filter","location","ValidupError"],"mappings":";;;;;;AASO,SAASA,qBAAAA,GAAAA;IACZ,OAAOC,qBAAAA,EAAAA;AACX;;ACIA,SAASC,uBAAAA,CACLC,KAAsB,EACtBC,OAAqB,EAAA;AAErB,IAAA,MAAMC,SAAmC,EAAE;AAC3C,IAAA,OAAQF,MAAMG,IAAI;QACd,KAAK,OAAA;AAAS,YAAA;AACV,gBAAA,MAAMC,IAAAA,GAAOJ,KAAAA,CAAMK,IAAI,IAAIJ,QAAQI,IAAI;AACvC,gBAAA,MAAMC,OAAAA,GAAUN,KAAAA,CAAMO,GAAG,IAAIC,sCAAAA,CAA+B;AAACJ,oBAAAA;AAAK,iBAAA,CAAA;gBAElEF,MAAAA,CAAOO,IAAI,CAAC,IAAIC,6BAAAA,CAAsB;oBAClCL,IAAAA,EAAMD,IAAAA;AACNO,oBAAAA,YAAAA,EAAcV,OAAAA,CAAQU,YAAY,IAAIV,OAAAA,CAAQI,IAAI;AAClDO,oBAAAA,QAAAA,EAAUZ,MAAMa,KAAK;AACrBP,oBAAAA;AACJ,iBAAA,CAAA,CAAA;AACA,gBAAA;AACJ,YAAA;QACA,KAAK,aAAA;AAAe,YAAA;gBAChB,IAAK,IAAIQ,IAAI,CAAA,EAAGA,CAAAA,GAAId,MAAMe,YAAY,CAACC,MAAM,EAAEF,CAAAA,EAAAA,CAAK;AAChDZ,oBAAAA,MAAAA,CAAOO,IAAI,CAAA,GAAIV,uBAAAA,CAAwBC,MAAMe,YAAY,CAACD,EAAE,EAAEb,OAAAA,CAAAA,CAAAA;AAClE,gBAAA;AACA,gBAAA;AACJ,YAAA;QACA,KAAK,qBAAA;AAAuB,YAAA;gBACxB,IAAK,IAAIa,IAAI,CAAA,EAAGA,CAAAA,GAAId,MAAMe,YAAY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F,UAAU,UAAA,EAAY;AAC7BE,YAAAA,MAAAA,GAASF,KAAAA,CAAMC,GAAAA,CAAAA;QACnB,CAAA,MAAO;YACHC,MAAAA,GAASF,KAAAA;AACb,QAAA;AAEA,QAAA,MAAMG,OAAAA,GAAU,MAAMD,MAAAA,CAAOE,GAAG,CAAC;AAC7BhC,YAAAA,IAAAA,EAAM6B,IAAId;AACd,SAAA,CAAA;AAEA,QAAA,MAAM,CAACkB,KAAAA,CAAM,GAAGF,QAAQG,OAAO,CAACC,OAAO,CAAC;YAAEC,YAAAA,EAAc;AAAM,SAAA,CAAA;AAC9D,QAAA,IAAIH,KAAAA,EAAO;YACP,MAAMZ,MAAAA,GAASgB,kBAAAA,CAAcN,OAAAA,CAAQG,OAAO,CAACb,MAAM,CAACiB,MAAM,CACtD,CAACpC,KAAAA,GAAUA,KAAAA,CAAMG,IAAI,KAAK,OAAA,IACtBH,KAAAA,CAAMqC,QAAQ,KAAKN,KAAAA,CAAMM,QAAQ,IACjCrC,KAAAA,CAAMK,IAAI,KAAK0B,KAAAA,CAAM1B,IAAI,CAAA,CAAA;YAGjC,IAAIc,MAAAA,CAAOH,MAAM,GAAG,CAAA,EAAG;AACnB,gBAAA,MAAME,iBAAiBC,MAAAA,EAAQ;AAC3Bd,oBAAAA,IAAAA,EAAMsB,IAAItB,IAAI;AACdM,oBAAAA,YAAAA,EAAcgB,IAAIhB;AACtB,iBAAA,CAAA;AACJ,YAAA;AAEA,YAAA,OAAOoB,MAAMlB,KAAK;AACtB,QAAA;QAEA,MAAM,IAAIyB,qBAAa,CAAC,cAAc,EAAEX,GAAAA,CAAItB,IAAI,CAAC,wBAAwB,CAAC,CAAA;AAC9E,IAAA,CAAA;AACJ;;;;;"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.mjs","sources":["../src/chain.ts","../src/error.ts","../src/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 { type ValidationChain, body } from 'express-validator';\n\nexport function createValidationChain() : ValidationChain {\n return body();\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 type { ValidationError } from 'express-validator/lib/base';\nimport { ValidupNestedError, ValidupValidatorError, buildErrorMessageForAttributes } from 'validup';\n\ntype ErrorOptions = {\n path: string,\n pathAbsolute?: string\n};\n\nfunction generateAttributeErrors(\n error: ValidationError,\n options: ErrorOptions,\n) : ValidupValidatorError[] {\n const output : ValidupValidatorError[] = [];\n switch (error.type) {\n case 'field': {\n const name = error.path || options.path;\n const message = error.msg || buildErrorMessageForAttributes([name]);\n\n output.push(new ValidupValidatorError({\n path: name,\n pathAbsolute: options.pathAbsolute || options.path,\n received: error.value,\n message,\n }));\n break;\n }\n case 'alternative': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i], options));\n }\n break;\n }\n case 'alternative_grouped': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n for (let j = 0; j < error.nestedErrors[i].length; j++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i][j], options));\n }\n }\n }\n }\n\n return output;\n}\n\nexport function buildNestedError(\n errors: ValidationError[],\n options: ErrorOptions,\n): ValidupNestedError {\n const base = new ValidupNestedError();\n const names : (number | string)[] = [];\n for (let i = 0; i < errors.length; i++) {\n const children = generateAttributeErrors(errors[i], options);\n for (let j = 0; j < children.length; j++) {\n base.addChild(children[j]);\n names.push(children[j].path);\n }\n }\n\n base.message = buildErrorMessageForAttributes(names);\n\n return base;\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 type { ContextRunner, FieldValidationError } from 'express-validator';\nimport { distinctArray } from 'smob';\nimport type { Validator, ValidatorContext } from 'validup';\nimport { ValidupError } from 'validup';\nimport { buildNestedError } from './error';\n\ntype ContextRunnerCreateFn = (\n ctx: ValidatorContext\n) => ContextRunner;\n\nexport function createValidator(\n input: ContextRunnerCreateFn | ContextRunner,\n) : Validator {\n return async (ctx): Promise<unknown> => {\n let runner : ContextRunner;\n if (typeof input === 'function') {\n runner = input(ctx);\n } else {\n runner = input;\n }\n\n const outcome = await runner.run({\n body: ctx.value,\n });\n\n const [field] = outcome.context.getData({ requiredOnly: false });\n if (field) {\n const errors = distinctArray(outcome.context.errors.filter(\n (error) => error.type === 'field' &&\n error.location === field.location &&\n error.path === field.path,\n ) as FieldValidationError[]);\n\n if (errors.length > 0) {\n throw buildNestedError(errors, {\n path: ctx.path,\n pathAbsolute: ctx.pathAbsolute,\n });\n }\n\n return field.value;\n }\n\n throw new ValidupError(`The attribute ${ctx.path} could not be validated.`);\n };\n}\n"],"names":["createValidationChain","body","generateAttributeErrors","error","options","output","type","name","path","message","msg","buildErrorMessageForAttributes","push","ValidupValidatorError","pathAbsolute","received","value","i","nestedErrors","length","j","buildNestedError","errors","base","ValidupNestedError","names","children","addChild","createValidator","input","ctx","runner","outcome","run","field","context","getData","requiredOnly","distinctArray","filter","location","ValidupError"],"mappings":";;;;AASO,SAASA,qBAAAA,GAAAA;IACZ,OAAOC,IAAAA,EAAAA;AACX;;ACIA,SAASC,uBAAAA,CACLC,KAAsB,EACtBC,OAAqB,EAAA;AAErB,IAAA,MAAMC,SAAmC,EAAE;AAC3C,IAAA,OAAQF,MAAMG,IAAI;QACd,KAAK,OAAA;AAAS,YAAA;AACV,gBAAA,MAAMC,IAAOJ,GAAAA,KAAAA,CAAMK,IAAI,IAAIJ,QAAQI,IAAI;AACvC,gBAAA,MAAMC,OAAUN,GAAAA,KAAAA,CAAMO,GAAG,IAAIC,8BAA+B,CAAA;AAACJ,oBAAAA;AAAK,iBAAA,CAAA;gBAElEF,MAAOO,CAAAA,IAAI,CAAC,IAAIC,qBAAsB,CAAA;oBAClCL,IAAMD,EAAAA,IA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A;AACnB,gBAAA,MAAME,iBAAiBC,MAAQ,EAAA;AAC3Bd,oBAAAA,IAAAA,EAAMsB,IAAItB,IAAI;AACdM,oBAAAA,YAAAA,EAAcgB,IAAIhB;AACtB,iBAAA,CAAA;AACJ;AAEA,YAAA,OAAOoB,MAAMlB,KAAK;AACtB;QAEA,MAAM,IAAIyB,aAAa,CAAC,cAAc,EAAEX,GAAItB,CAAAA,IAAI,CAAC,wBAAwB,CAAC,CAAA;AAC9E,KAAA;AACJ;;;;"} | ||
| {"version":3,"file":"index.mjs","sources":["../src/chain.ts","../src/error.ts","../src/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 { type ValidationChain, body } from 'express-validator';\n\nexport function createValidationChain() : ValidationChain {\n return body();\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 type { ValidationError } from 'express-validator/lib/base';\nimport { ValidupNestedError, ValidupValidatorError, buildErrorMessageForAttributes } from 'validup';\n\ntype ErrorOptions = {\n path: string,\n pathAbsolute?: string\n};\n\nfunction generateAttributeErrors(\n error: ValidationError,\n options: ErrorOptions,\n) : ValidupValidatorError[] {\n const output : ValidupValidatorError[] = [];\n switch (error.type) {\n case 'field': {\n const name = error.path || options.path;\n const message = error.msg || buildErrorMessageForAttributes([name]);\n\n output.push(new ValidupValidatorError({\n path: name,\n pathAbsolute: options.pathAbsolute || options.path,\n received: error.value,\n message,\n }));\n break;\n }\n case 'alternative': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i], options));\n }\n break;\n }\n case 'alternative_grouped': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n for (let j = 0; j < error.nestedErrors[i].length; j++) {\n output.push(...generateAttributeErrors(error.nestedErrors[i][j], options));\n }\n }\n }\n }\n\n return output;\n}\n\nexport function buildNestedError(\n errors: ValidationError[],\n options: ErrorOptions,\n): ValidupNestedError {\n const base = new ValidupNestedError();\n const names : (number | string)[] = [];\n for (let i = 0; i < errors.length; i++) {\n const children = generateAttributeErrors(errors[i], options);\n for (let j = 0; j < children.length; j++) {\n base.addChild(children[j]);\n names.push(children[j].path);\n }\n }\n\n base.message = buildErrorMessageForAttributes(names);\n\n return base;\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 type { ContextRunner, FieldValidationError } from 'express-validator';\nimport { distinctArray } from 'smob';\nimport type { Validator, ValidatorContext } from 'validup';\nimport { ValidupError } from 'validup';\nimport { buildNestedError } from './error';\n\ntype ContextRunnerCreateFn = (\n ctx: ValidatorContext\n) => ContextRunner;\n\nexport function createValidator(\n input: ContextRunnerCreateFn | ContextRunner,\n) : Validator {\n return async (ctx): Promise<unknown> => {\n let runner : ContextRunner;\n if (typeof input === 'function') {\n runner = input(ctx);\n } else {\n runner = input;\n }\n\n const outcome = await runner.run({\n body: ctx.value,\n });\n\n const [field] = outcome.context.getData({ requiredOnly: false });\n if (field) {\n const errors = distinctArray(outcome.context.errors.filter(\n (error) => error.type === 'field' &&\n error.location === field.location &&\n error.path === field.path,\n ) as FieldValidationError[]);\n\n if (errors.length > 0) {\n throw buildNestedError(errors, {\n path: ctx.path,\n pathAbsolute: ctx.pathAbsolute,\n });\n }\n\n return field.value;\n }\n\n throw new ValidupError(`The attribute ${ctx.path} could not be validated.`);\n };\n}\n"],"names":["createValidationChain","body","generateAttributeErrors","error","options","output","type","name","path","message","msg","buildErrorMessageForAttributes","push","ValidupValidatorError","pathAbsolute","received","value","i","nestedErrors","length","j","buildNestedError","errors","base","ValidupNestedError","names","children","addChild","createValidator","input","ctx","runner","outcome","run","field","context","getData","requiredOnly","distinctArray","filter","location","ValidupError"],"mappings":";;;;AASO,SAASA,qBAAAA,GAAAA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,CAAC;AAC7BhC,YAAAA,IAAAA,EAAM6B,IAAId;AACd,SAAA,CAAA;AAEA,QAAA,MAAM,CAACkB,KAAAA,CAAM,GAAGF,QAAQG,OAAO,CAACC,OAAO,CAAC;YAAEC,YAAAA,EAAc;AAAM,SAAA,CAAA;AAC9D,QAAA,IAAIH,KAAAA,EAAO;YACP,MAAMZ,MAAAA,GAASgB,aAAAA,CAAcN,OAAAA,CAAQG,OAAO,CAACb,MAAM,CAACiB,MAAM,CACtD,CAACpC,KAAAA,GAAUA,KAAAA,CAAMG,IAAI,KAAK,OAAA,IACtBH,KAAAA,CAAMqC,QAAQ,KAAKN,KAAAA,CAAMM,QAAQ,IACjCrC,KAAAA,CAAMK,IAAI,KAAK0B,KAAAA,CAAM1B,IAAI,CAAA,CAAA;YAGjC,IAAIc,MAAAA,CAAOH,MAAM,GAAG,CAAA,EAAG;AACnB,gBAAA,MAAME,iBAAiBC,MAAAA,EAAQ;AAC3Bd,oBAAAA,IAAAA,EAAMsB,IAAItB,IAAI;AACdM,oBAAAA,YAAAA,EAAcgB,IAAIhB;AACtB,iBAAA,CAAA;AACJ,YAAA;AAEA,YAAA,OAAOoB,MAAMlB,KAAK;AACtB,QAAA;QAEA,MAAM,IAAIyB,aAAa,CAAC,cAAc,EAAEX,GAAAA,CAAItB,IAAI,CAAC,wBAAwB,CAAC,CAAA;AAC9E,IAAA,CAAA;AACJ;;;;"} |
+4
-4
| { | ||
| "name": "@validup/adapter-validator", | ||
| "version": "0.2.0", | ||
| "version": "0.2.1", | ||
| "description": "A validator.js adapter for validup.", | ||
@@ -43,11 +43,11 @@ "author": { | ||
| "dependencies": { | ||
| "validup": "^0.1.8", | ||
| "validup": "^0.1.9", | ||
| "smob": "^1.5.0" | ||
| }, | ||
| "devDependencies": { | ||
| "express-validator": "^7.2.0" | ||
| "express-validator": "^7.2.1" | ||
| }, | ||
| "peerDependencies": { | ||
| "express-validator": "^7.2.0" | ||
| "express-validator": "^7.2.1" | ||
| } | ||
| } |
24792
0.71%Updated