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

Remove internal enum values from validation message #713

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
import software.amazon.smithy.model.shapes.SimpleShape;
import software.amazon.smithy.model.shapes.StringShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.EnumDefinition;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.EnumValueTrait;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.IdempotencyTokenTrait;
import software.amazon.smithy.model.traits.InternalTrait;
import software.amazon.smithy.model.traits.LengthTrait;
import software.amazon.smithy.model.traits.MediaTypeTrait;
import software.amazon.smithy.model.traits.PatternTrait;
Expand Down Expand Up @@ -503,6 +506,22 @@ private void writeShapeValidator(TypeScriptWriter writer,
});
}

if (shape.isEnumShape()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In smithy-typescript, the code generator does not convert all enum traits to enum shapes, so we have to check the EnumTrait like before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in dc90a62.

writer.addImport("EnumValidator", "__EnumValidator", "@aws-smithy/server-common");
Collection<MemberShape> enumValues = shape.asEnumShape().get().getAllMembers().values();
writer.openBlock("new __EnumValidator([", "]),", () -> {
for (MemberShape member : enumValues) {
writer.write("$S,", member.expectTrait(EnumValueTrait.class).expectStringValue());
}
writer.write("], [");
for (MemberShape member : shape.asEnumShape().get().getAllMembers().values()) {
if (!member.hasTrait((InternalTrait.class)) && !member.hasTag("internal")) {
writer.write("$S,", member.expectTrait(EnumValueTrait.class).expectStringValue());
}
}
});
}

for (Trait t : constraints) {
writeSingleConstraintValidator(writer, t);
}
Expand All @@ -517,12 +536,18 @@ private void writeSingleConstraintValidator(TypeScriptWriter writer, Trait trait
if (trait instanceof RequiredTrait) {
writer.addImport("RequiredValidator", "__RequiredValidator", "@aws-smithy/server-common");
writer.write("new __RequiredValidator(),");
} else if (trait instanceof EnumTrait) {
} else if (trait instanceof EnumTrait && !trait.isSynthetic()) {
writer.addImport("EnumValidator", "__EnumValidator", "@aws-smithy/server-common");
writer.openBlock("new __EnumValidator([", "]),", () -> {
for (String e : ((EnumTrait) trait).getEnumDefinitionValues()) {
writer.write("$S,", e);
}
writer.write("], [");
for (EnumDefinition enumDefinition : ((EnumTrait) trait).getValues()) {
if (!enumDefinition.hasTag("internal")) {
writer.write("$S, ", enumDefinition.getValue());
}
}
});
} else if (trait instanceof LengthTrait) {
LengthTrait lengthTrait = (LengthTrait) trait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("sensitive validation", () => {

describe("strips the failure value from the resultant validation failure", () => {
it("with enums", () => {
expect(sensitize(new EnumValidator(["apple", "banana", "orange"]), "pear").failureValue).toBeNull();
expect(sensitize(new EnumValidator(["apple", "banana", "orange"], ["apple"]), "pear").failureValue).toBeNull();
});
it("with integer enums", () => {
expect(sensitize(new IntegerEnumValidator([1, 2, 3]), 0).failureValue).toBeNull();
Expand All @@ -53,7 +53,7 @@ describe("sensitive validation", () => {
});

describe("enum validation", () => {
const enumValidator = new EnumValidator(["apple", "banana", "orange"]);
const enumValidator = new EnumValidator(["apple", "banana", "orange"], ["apple", "banana"]);

it("does not fail when the enum value is found", () => {
expect(enumValidator.validate("apple", "fruit")).toBeNull();
Expand All @@ -62,7 +62,7 @@ describe("enum validation", () => {
it("fails when the enum value is not found", () => {
expect(enumValidator.validate("kiwi", "fruit")).toEqual({
constraintType: "enum",
constraintValues: ["apple", "banana", "orange"],
constraintValues: ["apple", "banana"],
path: "fruit",
failureValue: "kiwi",
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ export interface SingleConstraintValidator<T, F> {

export class EnumValidator implements SingleConstraintValidator<string, EnumValidationFailure> {
private readonly allowedValues: string[];
private readonly nonInternalValues: string[];

constructor(allowedValues: readonly string[]) {
constructor(allowedValues: readonly string[], nonInternalValues: readonly string[]) {
this.allowedValues = allowedValues.slice();
this.nonInternalValues = nonInternalValues.slice();
}

validate(input: string | undefined | null, path: string): EnumValidationFailure | null {
Expand All @@ -166,7 +168,7 @@ export class EnumValidator implements SingleConstraintValidator<string, EnumVali
if (this.allowedValues.indexOf(input) < 0) {
return {
constraintType: "enum",
constraintValues: this.allowedValues.slice(),
constraintValues: this.nonInternalValues.slice(),
path: path,
failureValue: input,
};
Expand Down