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

Add intEnum validation to NodeValidationVisitor #2357

Merged
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
@@ -0,0 +1,32 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.model.validation.node;

import java.util.Collection;
import software.amazon.smithy.model.node.NumberNode;
import software.amazon.smithy.model.shapes.IntEnumShape;
import software.amazon.smithy.model.validation.ValidationUtils;


/**
* Validates NumberNodes against intEnum shapes' allowed enum values.
*/
final class IntEnumPlugin extends FilteredPlugin<IntEnumShape, NumberNode> {

IntEnumPlugin() {
super(IntEnumShape.class, NumberNode.class);
}

@Override
protected void check(IntEnumShape shape, NumberNode node, Context context, Emitter emitter) {
Collection<Integer> values = shape.getEnumValues().values();
if (!values.contains(node.getValue().intValue())) {
emitter.accept(node, String.format(
"Integer value provided for `%s` must be one of the following values: %s, but found %s",
shape.getId(), ValidationUtils.tickedList(values), node.getValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static List<NodeValidatorPlugin> getBuiltins() {
new PatternTraitPlugin(),
new RangeTraitPlugin(),
new StringEnumPlugin(),
new IntEnumPlugin(),
new StringLengthPlugin(),
new UniqueItemsPlugin());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ public static Collection<Object[]> data() {
{"ns.foo#Integer", "9", new String[] {"Value provided for `ns.foo#Integer` must be greater than or equal to 10, but found 9"}},
{"ns.foo#Integer", "10.2", new String[] {"integer shapes must not have floating point values, but found `10.2` provided for `ns.foo#Integer`"}},

// intEnum
{"ns.foo#IntEnum", "1", null},
{"ns.foo#IntEnum", "2", null},
{"ns.foo#IntEnum", "3", new String[] {"Integer value provided for `ns.foo#IntEnum` must be one of the following values: `1`, `2`, but found 3"}},
{"ns.foo#IntEnum", "true", new String[] {"Expected number value for intEnum shape, `ns.foo#IntEnum`; found boolean value, `true`"}},
{"ns.foo#IntEnum", "1.1", new String[] {"intEnum shapes must not have floating point values, but found `1.1` provided for `ns.foo#IntEnum`"}},

// long
{"ns.foo#Long", "10", null},
{"ns.foo#Long", "true", new String[] {"Expected number value for long shape, `ns.foo#Long`; found boolean value, `true`"}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
}
}
},
"ns.foo#IntEnum": {
"type": "intEnum",
"members": {
"V1": {
"target": "smithy.api#Unit",
"traits": {
"smithy.api#enumValue": 1
}
},
"V2": {
"target": "smithy.api#Unit",
"traits": {
"smithy.api#enumValue": 2
}
}
}
},
"ns.foo#Long": {
"type": "long",
"traits": {
Expand Down
Loading