Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/report failed conditions #1130

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add condition to errors, no tests yet
  • Loading branch information
amagid committed Feb 13, 2022
commit cc0b577c2997bf07b75db9dfbe006c9b7c4d3c90
3 changes: 3 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReadonlyContext } from './context';
import { ContextItem } from './context-items';

export interface Request {
[k: string]: any;
Expand Down Expand Up @@ -38,6 +39,7 @@ export type ValidationError =
// These are optional so places don't need to define them, but can reference them
location?: undefined;
value?: undefined;
condition?: ContextItem;
}
| {
location: Location;
Expand All @@ -46,6 +48,7 @@ export type ValidationError =
msg: any;
// This is optional so places don't need to define it, but can reference it
nestedErrors?: unknown[];
condition?: ContextItem;
};

// Not using Symbol because of #813
Expand Down
9 changes: 7 additions & 2 deletions src/context-items/custom-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ export class CustomValidation implements ContextItem {
// A promise that was resolved only adds an error if negated.
// Otherwise it always suceeds
if ((!isPromise && failed) || (isPromise && this.negated)) {
context.addError(this.message, value, meta);
context.addError(this.message, value, meta, this);
}
} catch (err) {
if (this.negated) {
return;
}

context.addError(this.message || (err instanceof Error ? err.message : err), value, meta);
context.addError(
this.message || (err instanceof Error ? err.message : err),
value,
meta,
this,
);
}
}
}
2 changes: 1 addition & 1 deletion src/context-items/standard-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class StandardValidation implements ContextItem {
async run(context: Context, value: any, meta: Meta) {
const result = this.validator(toString(value), ...this.options);
if (this.negated ? result : !result) {
context.addError(this.message, value, meta);
context.addError(this.message, value, meta, this);
}
}
}
21 changes: 15 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,32 @@ export class Context {
instance.value = value;
}

addError(message: any, value: any, meta: Meta): void;
addError(message: any, value: any, meta: Meta, condition: ContextItem): void;
addError(message: any, nestedErrors: ValidationError[]): void;
addError(message: any, valueOrNestedErrors: any, meta?: Meta) {
addError(message: any, valueOrNestedErrors: any, meta?: Meta, condition?: ContextItem) {
const msg = message || this.message || 'Invalid value';
let obj: ValidationError;
if (meta) {
this._errors.push({
obj = {
value: valueOrNestedErrors,
msg: typeof msg === 'function' ? msg(valueOrNestedErrors, meta) : msg,
param: meta.path,
location: meta.location,
});
};
if (condition) {
Object.defineProperty(obj, 'condition', { value: condition });
}
this._errors.push(obj);
} else {
this._errors.push({
obj = {
msg,
param: '_error',
nestedErrors: valueOrNestedErrors,
});
};
if (condition) {
Object.defineProperty(obj, 'condition', { value: condition });
}
this._errors.push(obj);
}
}
}
Expand Down