Skip to content

Commit

Permalink
Add NodeValidationVisitor plugin for @Uniqueitems
Browse files Browse the repository at this point in the history
  • Loading branch information
kstich committed Nov 7, 2023
1 parent 78f336f commit 1fdd0ae
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ static List<NodeValidatorPlugin> getBuiltins() {
new PatternTraitPlugin(),
new RangeTraitPlugin(),
new StringEnumPlugin(),
new StringLengthPlugin());
new StringLengthPlugin(),
new UniqueItemsPlugin());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.CollectionShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.UniqueItemsTrait;

/**
* Validates that items in lists with the `@uniqueItems` trait are unique.
*/
final class UniqueItemsPlugin extends MemberAndShapeTraitPlugin<CollectionShape, ArrayNode, UniqueItemsTrait> {
UniqueItemsPlugin() {
super(CollectionShape.class, ArrayNode.class, UniqueItemsTrait.class);
}

@Override
protected void check(Shape shape, UniqueItemsTrait trait, ArrayNode value, Context context, Emitter emitter) {
Set<Node> uniqueNodes = new HashSet<>(value.getElements());
if (uniqueNodes.size() != value.size()) {
List<Node> duplicateNodes = new ArrayList<>(value.getElements());
for (Node uniqueNode : uniqueNodes) {
// Collections remove the first encountered entry, so our unique
// nodes through a set will leave behind any duplicates.
duplicateNodes.remove(uniqueNode);
}

Set<String> duplicateValues = new LinkedHashSet<>();
for (Node duplicateNode : duplicateNodes) {
duplicateValues.add(duplicateNode.toString());
}
emitter.accept(value, String.format(
"Value provided for `%s` must have unique items, but the following items had multiple entries: "
+ "[`%s`]", shape.getId(), String.join("`, `", duplicateValues)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ public static Collection<Object[]> data() {
{"ns.foo#List", "[10]", new String[] {"0: Expected string value for string shape, `ns.foo#String`; found number value, `10`"}},
{"ns.foo#List", "10", new String[] {"Expected array value for list shape, `ns.foo#List`; found number value, `10`"}},

// unique list
{"ns.foo#UniqueList", "[\"a\"]", null},
{"ns.foo#UniqueList", "[\"a\", \"b\"]", null},
{"ns.foo#UniqueList", "[\"a\", \"a\"]", new String[] {"Value provided for `ns.foo#UniqueList` must have unique items, but the following items had multiple entries: [`a`]"}},
{"ns.foo#UniqueList", "[\"a\", \"a\", \"a\"]", new String[] {"Value provided for `ns.foo#UniqueList` must have unique items, but the following items had multiple entries: [`a`]"}},
{"ns.foo#UniqueList", "[\"a\", \"a\", \"b\", \"b\"]", new String[] {"Value provided for `ns.foo#UniqueList` must have unique items, but the following items had multiple entries: [`a`, `b`]"}},

// map
{"ns.foo#Map", "{\"a\":[\"b\"]}", null},
{"ns.foo#Map", "{\"a\":[\"b\"], \"c\":[\"d\"]}", null},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@
}
}
},
"ns.foo#UniqueList": {
"type": "list",
"member": {
"target": "ns.foo#String"
},
"traits": {
"smithy.api#uniqueItems": {}
}
},
"ns.foo#String": {
"type": "string"
},
Expand Down

0 comments on commit 1fdd0ae

Please sign in to comment.