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 NodeValidationVisitor plugin for @uniqueItems #2031

Merged
merged 1 commit into from
Nov 7, 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 @@ -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
Loading