New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@validup/adapter-validator

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@validup/adapter-validator - npm Package Compare versions

Comparing version
0.2.2
to
0.3.0
+2
-7
dist/error.d.ts
import type { ValidationError } from 'express-validator/lib/base';
import { ValidupNestedError } from 'validup';
type ErrorOptions = {
path: string;
pathAbsolute?: string;
};
export declare function buildNestedError(errors: ValidationError[], options: ErrorOptions): ValidupNestedError;
export {};
import type { Issue } from 'validup';
export declare function buildIssuesForErrors(errors: ValidationError[]): Issue[];

@@ -11,3 +11,3 @@ 'use strict';

function generateAttributeErrors(error, options) {
function buildIssuesForError(error) {
const output = [];

@@ -17,11 +17,8 @@ switch(error.type){

{
const name = error.path || options.path;
const message = error.msg || validup.buildErrorMessageForAttributes([
name
]);
output.push(new validup.ValidupValidatorError({
path: name,
pathAbsolute: options.pathAbsolute || options.path,
output.push(validup.defineIssueItem({
path: error.path ? [
error.path
] : [],
received: error.value,
message
message: error.msg
}));

@@ -33,3 +30,3 @@ break;

for(let i = 0; i < error.nestedErrors.length; i++){
output.push(...generateAttributeErrors(error.nestedErrors[i], options));
output.push(...buildIssuesForError(error.nestedErrors[i]));
}

@@ -42,3 +39,3 @@ break;

for(let j = 0; j < error.nestedErrors[i].length; j++){
output.push(...generateAttributeErrors(error.nestedErrors[i][j], options));
output.push(...buildIssuesForError(error.nestedErrors[i][j]));
}

@@ -50,14 +47,8 @@ }

}
function buildNestedError(errors, options) {
const base = new validup.ValidupNestedError();
const names = [];
function buildIssuesForErrors(errors) {
const issues = [];
for(let i = 0; i < errors.length; i++){
const children = generateAttributeErrors(errors[i], options);
for(let j = 0; j < children.length; j++){
base.addChild(children[j]);
names.push(children[j].path);
}
issues.push(...buildIssuesForError(errors[i]));
}
base.message = validup.buildErrorMessageForAttributes(names);
return base;
return issues;
}

@@ -76,2 +67,3 @@

});
const issues = [];
const [field] = outcome.context.getData({

@@ -83,10 +75,8 @@ requiredOnly: false

if (errors.length > 0) {
throw buildNestedError(errors, {
path: ctx.path,
pathAbsolute: ctx.pathAbsolute
});
issues.push(...buildIssuesForErrors(errors));
} else {
return field.value;
}
return field.value;
}
throw new validup.ValidupError(`The attribute ${ctx.path} could not be validated.`);
throw new validup.ValidupError(issues);
};

@@ -93,0 +83,0 @@ }

@@ -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,qB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,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;;;;;"}
{"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 type { Issue } from 'validup';\nimport { defineIssueItem } from 'validup';\n\nfunction buildIssuesForError(\n error: ValidationError,\n) : Issue[] {\n const output : Issue[] = [];\n switch (error.type) {\n case 'field': {\n output.push(defineIssueItem({\n path: error.path ? [error.path] : [],\n received: error.value,\n message: error.msg,\n }));\n break;\n }\n case 'alternative': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n output.push(...buildIssuesForError(error.nestedErrors[i]));\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(...buildIssuesForError(error.nestedErrors[i][j]));\n }\n }\n }\n }\n\n return output;\n}\n\nexport function buildIssuesForErrors(\n errors: ValidationError[],\n): Issue[] {\n const issues : Issue[] = [];\n for (let i = 0; i < errors.length; i++) {\n issues.push(...buildIssuesForError(errors[i]));\n }\n\n return issues;\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 { Issue, Validator, ValidatorContext } from 'validup';\nimport { ValidupError } from 'validup';\nimport { buildIssuesForErrors } 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 issues : Issue[] = [];\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 issues.push(...buildIssuesForErrors(errors));\n } else {\n return field.value;\n }\n }\n\n throw new ValidupError(issues);\n };\n}\n"],"names":["createValidationChain","body","buildIssuesForError","error","output","type","push","defineIssueItem","path","received","value","message","msg","i","nestedErrors","length","j","buildIssuesForErrors","errors","issues","createValidator","input","ctx","runner","outcome","run","field","context","getData","requiredOnly","distinctArray","filter","location","ValidupError"],"mappings":";;;;;;AASO,SAASA,qBAAAA,GAAAA;IACZ,OAAOC,qBAAAA,EAAAA;AACX;;ACAA,SAASC,oBACLC,KAAsB,EAAA;AAEtB,IAAA,MAAMC,SAAmB,EAAE;AAC3B,IAAA,OAAQD,MAAME,IAAI;QACd,KAAK,OAAA;AAAS,YAAA;gBACVD,MAAAA,CAAOE,IAAI,CAACC,uBAAAA,CAAgB;oBACxBC,IAAAA,EAAML,KAAAA,CAAMK,IAAI,GAAG;AAACL,wBAAAA,KAAAA,CAAMK;AAAK,qBAAA,GAAG,EAAE;AACpCC,oBAAAA,QAAAA,EAAUN,MAAMO,KAAK;AACrBC,oBAAAA,OAAAA,EAASR,MAAMS;AACnB,iBAAA,CAAA,CAAA;AACA,gBAAA;AACJ,YAAA;QACA,KAAK,aAAA;AAAe,YAAA;gBAChB,IAAK,IAAIC,IAAI,CAAA,EAAGA,CAAAA,GAAIV,MAAMW,YAAY,CAACC,MAAM,EAAEF,CAAAA,EAAAA,CAAK;AAChDT,oBAAAA,MAAAA,CAAOE,IAAI,CAAA,GAAIJ,mBAAAA,CAAoBC,KAAAA,CAAMW,YAAY,CAACD,CAAAA,CAAE,CAAA,CAAA;AAC5D,gBAAA;AACA,gBAAA;AACJ,YAAA;QACA,KAAK,qBAAA;AAAuB,YAAA;gBACxB,IAAK,IAAIA,IAAI,CAAA,EAAGA,CAAAA,GAAIV,MAAMW,YAAY,CAACC,MAAM,EAAEF,CAAAA,EAAAA,CAAK;oBAChD,IAAK,IAAIG,CAAAA,GAAI,CAAA,EAAGA,CAAAA,GAAIb,KAAAA,CAAMW,YAAY,CAACD,CAAAA,CAAE,CAACE,MAAM,EAAEC,CAAAA,EAAAA,CAAK;wBACnDZ,MA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KN,KAAAA,CAAMM,QAAQ,IACjC7B,KAAAA,CAAMK,IAAI,KAAKkB,KAAAA,CAAMlB,IAAI,CAAA,CAAA;YAGjC,IAAIU,MAAAA,CAAOH,MAAM,GAAG,CAAA,EAAG;gBACnBI,MAAAA,CAAOb,IAAI,IAAIW,oBAAAA,CAAqBC,MAAAA,CAAAA,CAAAA;YACxC,CAAA,MAAO;AACH,gBAAA,OAAOQ,MAAMhB,KAAK;AACtB,YAAA;AACJ,QAAA;AAEA,QAAA,MAAM,IAAIuB,oBAAAA,CAAad,MAAAA,CAAAA;AAC3B,IAAA,CAAA;AACJ;;;;;"}
import { body } from 'express-validator';
import { distinctArray } from 'smob';
import { ValidupNestedError, buildErrorMessageForAttributes, ValidupValidatorError, ValidupError } from 'validup';
import { defineIssueItem, ValidupError } from 'validup';

@@ -9,3 +9,3 @@ function createValidationChain() {

function generateAttributeErrors(error, options) {
function buildIssuesForError(error) {
const output = [];

@@ -15,11 +15,8 @@ switch(error.type){

{
const name = error.path || options.path;
const message = error.msg || buildErrorMessageForAttributes([
name
]);
output.push(new ValidupValidatorError({
path: name,
pathAbsolute: options.pathAbsolute || options.path,
output.push(defineIssueItem({
path: error.path ? [
error.path
] : [],
received: error.value,
message
message: error.msg
}));

@@ -31,3 +28,3 @@ break;

for(let i = 0; i < error.nestedErrors.length; i++){
output.push(...generateAttributeErrors(error.nestedErrors[i], options));
output.push(...buildIssuesForError(error.nestedErrors[i]));
}

@@ -40,3 +37,3 @@ break;

for(let j = 0; j < error.nestedErrors[i].length; j++){
output.push(...generateAttributeErrors(error.nestedErrors[i][j], options));
output.push(...buildIssuesForError(error.nestedErrors[i][j]));
}

@@ -48,14 +45,8 @@ }

}
function buildNestedError(errors, options) {
const base = new ValidupNestedError();
const names = [];
function buildIssuesForErrors(errors) {
const issues = [];
for(let i = 0; i < errors.length; i++){
const children = generateAttributeErrors(errors[i], options);
for(let j = 0; j < children.length; j++){
base.addChild(children[j]);
names.push(children[j].path);
}
issues.push(...buildIssuesForError(errors[i]));
}
base.message = buildErrorMessageForAttributes(names);
return base;
return issues;
}

@@ -74,2 +65,3 @@

});
const issues = [];
const [field] = outcome.context.getData({

@@ -81,10 +73,8 @@ requiredOnly: false

if (errors.length > 0) {
throw buildNestedError(errors, {
path: ctx.path,
pathAbsolute: ctx.pathAbsolute
});
issues.push(...buildIssuesForErrors(errors));
} else {
return field.value;
}
return field.value;
}
throw new ValidupError(`The attribute ${ctx.path} could not be validated.`);
throw new ValidupError(issues);
};

@@ -91,0 +81,0 @@ }

@@ -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"],"mapp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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,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;;;;"}
{"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 type { Issue } from 'validup';\nimport { defineIssueItem } from 'validup';\n\nfunction buildIssuesForError(\n error: ValidationError,\n) : Issue[] {\n const output : Issue[] = [];\n switch (error.type) {\n case 'field': {\n output.push(defineIssueItem({\n path: error.path ? [error.path] : [],\n received: error.value,\n message: error.msg,\n }));\n break;\n }\n case 'alternative': {\n for (let i = 0; i < error.nestedErrors.length; i++) {\n output.push(...buildIssuesForError(error.nestedErrors[i]));\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(...buildIssuesForError(error.nestedErrors[i][j]));\n }\n }\n }\n }\n\n return output;\n}\n\nexport function buildIssuesForErrors(\n errors: ValidationError[],\n): Issue[] {\n const issues : Issue[] = [];\n for (let i = 0; i < errors.length; i++) {\n issues.push(...buildIssuesForError(errors[i]));\n }\n\n return issues;\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 { Issue, Validator, ValidatorContext } from 'validup';\nimport { ValidupError } from 'validup';\nimport { buildIssuesForErrors } 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 issues : Issue[] = [];\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 issues.push(...buildIssuesForErrors(errors));\n } else {\n return field.value;\n }\n }\n\n throw new ValidupError(issues);\n };\n}\n"],"names":["createValidationChain","body","buildIssuesForError","error","output","type","push","defineIssueItem","path","received","value","message","msg","i","nestedErrors","length","j","buildIssuesForErrors","errors","issues","createValidator","input","ctx","runner","outcome","run","field","context","getData","requiredOnly","distinctArray","filter","location","ValidupError"],"mappings":";;;;AASO,SAASA,qBAAAA,GAAAA;IACZ,OAAOC,IAAAA,EAAAA;AACX;;ACAA,SAASC,oBACLC,KAAsB,EAAA;AAEtB,IAAA,MAAMC,SAAmB,EAAE;AAC3B,IAAA,OAAQD,MAAME,IAAI;QACd,KAAK,OAAA;AAAS,YAAA;gBACVD,MAAAA,CAAOE,IAAI,CAACC,eAAAA,CAAgB;oBACxBC,IAAAA,EAAML,KAAAA,CAAMK,IAAI,GAAG;AAACL,wBAAAA,KAAAA,CAAMK;AAAK,qBAAA,GAAG,EAAE;AACpCC,oBAAAA,QAAAA,EAAUN,MAAMO,KAAK;AACrBC,oBAAAA,OAAAA,EAASR,MAAMS;AACnB,iBAAA,CAAA,CAAA;AACA,gBAAA;AACJ,YAAA;QACA,KAAK,aAAA;AAAe,YAAA;gBAChB,IAAK,IAAIC,IAAI,CAAA,EAAGA,CAAAA,GAAIV,MAAMW,YAAY,CAACC,MAAM,EAAEF,CAAAA,EAAAA,CAAK;AAChDT,oBAAAA,MAAAA,CAAOE,IAAI,CAAA,GAAIJ,mBAAAA,CAAoBC,KAAAA,CAAMW,YAAY,CAACD,CAAAA,CAAE,CAAA,CAAA;AAC5D,gBAAA;AACA,gBAAA;AACJ,YAAA;QACA,KAAK,qBAAA;AAAuB,YAAA;gBACxB,IAAK,IAAIA,IAAI,CAAA,EAAGA,CAAAA,GAAIV,MAAMW,YAAY,CAACC,MAAM,EAAEF,CAAAA,EAAAA,CAAK;oBAChD,IAAK,IAAIG,CAAAA,GAAI,CAAA,EAAGA,CAAAA,GAAIb,KAAAA,CAAMW,YAAY,CAACD,CAAAA,CAAE,CAACE,MAAM,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AAA,IACtBF,KAAAA,CAAM6B,QAAQ,KAAKN,KAAAA,CAAMM,QAAQ,IACjC7B,KAAAA,CAAMK,IAAI,KAAKkB,KAAAA,CAAMlB,IAAI,CAAA,CAAA;YAGjC,IAAIU,MAAAA,CAAOH,MAAM,GAAG,CAAA,EAAG;gBACnBI,MAAAA,CAAOb,IAAI,IAAIW,oBAAAA,CAAqBC,MAAAA,CAAAA,CAAAA;YACxC,CAAA,MAAO;AACH,gBAAA,OAAOQ,MAAMhB,KAAK;AACtB,YAAA;AACJ,QAAA;AAEA,QAAA,MAAM,IAAIuB,YAAAA,CAAad,MAAAA,CAAAA;AAC3B,IAAA,CAAA;AACJ;;;;"}
{
"name": "@validup/adapter-validator",
"version": "0.2.2",
"version": "0.3.0",
"description": "A validator.js adapter for validup.",

@@ -43,3 +43,3 @@ "author": {

"dependencies": {
"validup": "^0.1.10",
"validup": "^0.2.0",
"smob": "^1.5.0"

@@ -52,3 +52,6 @@ },

"express-validator": "^7.3.1"
},
"publishConfig": {
"access": "public"
}
}