Skip to content

Commit

Permalink
Remove reflected values from validation message (#695)
Browse files Browse the repository at this point in the history
* Remove reflected values from validation message
  • Loading branch information
srchase authored Feb 27, 2023
1 parent a179a35 commit 9eb56cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
PatternValidationFailure,
RangeValidationFailure,
RequiredValidationFailure,
UniqueItemsValidationFailure,
} from "./index";

describe("message formatting", () => {
Expand All @@ -32,8 +33,7 @@ describe("message formatting", () => {
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz... " +
"(truncated) at '/test' failed to satisfy constraint: Member must satisfy regular expression pattern: ^[a-c]$"
"Value at '/test' failed to satisfy constraint: Member must satisfy regular expression pattern: ^[a-c]$"
);
});
it("omits null values", () => {
Expand All @@ -50,7 +50,7 @@ describe("message formatting", () => {
it("formats required failures", () => {
const failure = new RequiredValidationFailure("/test");
expect(generateValidationMessage(failure)).toEqual(
"Value null at '/test' failed to satisfy constraint: Member must not be null"
"Value at '/test' failed to satisfy constraint: Member must not be null"
);
});
it("formats enum failures", () => {
Expand All @@ -61,7 +61,7 @@ describe("message formatting", () => {
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value pear at '/test' failed to satisfy constraint: Member must satisfy enum value set: [apple, banana]"
"Value at '/test' failed to satisfy constraint: Member must satisfy enum value set: [apple, banana]"
);
});
it("formats integer enum failures", () => {
Expand All @@ -72,7 +72,7 @@ describe("message formatting", () => {
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value 3 at '/test' failed to satisfy constraint: Member must satisfy enum value set: [1, 2]"
"Value at '/test' failed to satisfy constraint: Member must satisfy enum value set: [1, 2]"
);
});
describe("formats length failures", () => {
Expand Down Expand Up @@ -118,7 +118,7 @@ describe("message formatting", () => {
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value xyz at '/test' failed to satisfy constraint: Member must satisfy regular expression pattern: ^[a-c]$"
"Value at '/test' failed to satisfy constraint: Member must satisfy regular expression pattern: ^[a-c]$"
);
});
describe("formats range failures", () => {
Expand All @@ -130,7 +130,7 @@ describe("message formatting", () => {
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value 3 at '/test' failed to satisfy constraint: Member must be greater than or equal to 7"
"Value at '/test' failed to satisfy constraint: Member must be greater than or equal to 7"
);
});
it("with only max values", () => {
Expand All @@ -141,7 +141,7 @@ describe("message formatting", () => {
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value 3 at '/test' failed to satisfy constraint: Member must be less than or equal to 2"
"Value at '/test' failed to satisfy constraint: Member must be less than or equal to 2"
);
});
it("with min and max values", () => {
Expand All @@ -152,7 +152,17 @@ describe("message formatting", () => {
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value 2 at '/test' failed to satisfy constraint: Member must be between 3 and 7, inclusive"
"Value at '/test' failed to satisfy constraint: Member must be between 3 and 7, inclusive"
);
});
it("with unique items", () => {
const failure: UniqueItemsValidationFailure = {
constraintType: "uniqueItems",
failureValue: [5, 9],
path: "/test",
};
expect(generateValidationMessage(failure)).toEqual(
"Value with repeated values at '/test' failed to satisfy constraint: Member must have unique values"
);
});
});
Expand Down
18 changes: 2 additions & 16 deletions smithy-typescript-ssdk-libs/server-common/src/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,6 @@ export const generateValidationSummary = (failures: readonly ValidationFailure[]
};

export const generateValidationMessage = (failure: ValidationFailure): string => {
let failureValue;
if (failure.constraintType === "required") {
failureValue = "null ";
} else if (failure.failureValue === null) {
failureValue = "";
} else {
const rawFailureValue = failure.failureValue.toString();
if (rawFailureValue.length > 64) {
failureValue = rawFailureValue.substr(0, 49) + "... (truncated) ";
} else {
failureValue = rawFailureValue + " ";
}
}

let prefix = "Value";
let suffix: string;
switch (failure.constraintType) {
Expand All @@ -136,7 +122,7 @@ export const generateValidationMessage = (failure: ValidationFailure): string =>
}
case "length": {
if (failure.failureValue !== null) {
prefix = prefix + " with length";
prefix = prefix + " with length " + failure.failureValue;
}
const min = failure.constraintValues[0];
const max = failure.constraintValues[1];
Expand Down Expand Up @@ -170,5 +156,5 @@ export const generateValidationMessage = (failure: ValidationFailure): string =>
suffix = "must have unique values";
}
}
return `${prefix} ${failureValue}at '${failure.path}' failed to satisfy constraint: Member ${suffix}`;
return `${prefix} at '${failure.path}' failed to satisfy constraint: Member ${suffix}`;
};

0 comments on commit 9eb56cc

Please sign in to comment.