Skip to content

Commit

Permalink
Add intEnum validation to NodeValidationVisitor (#2357)
Browse files Browse the repository at this point in the history
* Add intEnum validation to NodeValidationVisitor

* Use new copyright notice

Co-authored-by: Manuel Sugawara <[email protected]>

---------

Co-authored-by: Maxim Korolkov <[email protected]>
Co-authored-by: Manuel Sugawara <[email protected]>
  • Loading branch information
3 people authored Jul 26, 2024
1 parent efb35c8 commit 31a6498
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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

0 comments on commit 31a6498

Please sign in to comment.