Skip to content

Commit 58ab086

Browse files
authored
uniqueItems property not working properly for arrays of objects (#216)
1 parent 2907ea6 commit 58ab086

File tree

3 files changed

+17
-39
lines changed

3 files changed

+17
-39
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parser/jsonParser.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export enum EnumMatch {
171171
Key, Enum
172172
}
173173

174-
const schemaDraftFromId : { [id:string]: SchemaDraft } = {
174+
const schemaDraftFromId: { [id: string]: SchemaDraft } = {
175175
'http://json-schema.org/draft-03/schema#': SchemaDraft.v3,
176176
'http://json-schema.org/draft-04/schema#': SchemaDraft.v4,
177177
'http://json-schema.org/draft-06/schema#': SchemaDraft.v6,
@@ -370,13 +370,13 @@ export class JSONDocument {
370370
return [];
371371
}
372372
}
373-
function getSchemaDraft(schema: JSONSchema, fallBack = SchemaDraft.v2020_12 ) {
373+
function getSchemaDraft(schema: JSONSchema, fallBack = SchemaDraft.v2020_12) {
374374
let schemaId = schema.$schema;
375375
if (schemaId) {
376376
return schemaDraftFromId[schemaId] ?? fallBack;
377377
}
378378
return fallBack;
379-
}
379+
}
380380

381381
function validate(n: ASTNode | undefined, schema: JSONSchema, validationResult: ValidationResult, matchingSchemas: ISchemaCollector, context: IEvaluationContext): void {
382382

@@ -855,10 +855,18 @@ function validate(n: ASTNode | undefined, schema: JSONSchema, validationResult:
855855

856856
if (schema.uniqueItems === true) {
857857
const values = getNodeValue(node);
858-
const duplicates = values.some((value: any, index: number) => {
859-
return index !== values.lastIndexOf(value);
860-
});
861-
if (duplicates) {
858+
function hasDuplicates() {
859+
for (let i = 0; i < values.length - 1; i++) {
860+
const value = values[i];
861+
for (let j = i + 1; j < values.length; j++) {
862+
if (equals(value, values[j])) {
863+
return true;
864+
}
865+
}
866+
}
867+
return false;
868+
}
869+
if (hasDuplicates()) {
862870
validationResult.problems.push({
863871
location: { offset: node.offset, length: node.length },
864872
message: l10n.t('Array has duplicate items.')

src/test/jsonSchemaTestSuite.test.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ const skippedTests = new Set([
122122
"draft4/refRemote.json/root ref in remote ref/string is valid",
123123
"draft4/refRemote.json/root ref in remote ref/null is valid",
124124
"draft4/refRemote.json/Location-independent identifier in remote ref/integer is valid",
125-
"draft4/uniqueItems.json/uniqueItems validation/non-unique array of objects is invalid",
126-
"draft4/uniqueItems.json/uniqueItems validation/non-unique array of nested objects is invalid",
127-
"draft4/uniqueItems.json/uniqueItems validation/non-unique array of arrays is invalid",
128-
"draft4/uniqueItems.json/uniqueItems validation/non-unique array of more than two arrays is invalid",
129-
"draft4/uniqueItems.json/uniqueItems validation/non-unique heterogeneous types are invalid",
130-
"draft4/uniqueItems.json/uniqueItems validation/objects are non-unique despite key order",
131125
"draft6/boolean_schema.json/boolean schema 'false'/number is invalid",
132126
"draft6/boolean_schema.json/boolean schema 'false'/string is invalid",
133127
"draft6/boolean_schema.json/boolean schema 'false'/boolean true is invalid",
@@ -160,12 +154,6 @@ const skippedTests = new Set([
160154
"draft6/refRemote.json/root ref in remote ref/null is valid",
161155
"draft6/refRemote.json/remote ref with ref to definitions/valid",
162156
"draft6/refRemote.json/Location-independent identifier in remote ref/integer is valid",
163-
"draft6/uniqueItems.json/uniqueItems validation/non-unique array of objects is invalid",
164-
"draft6/uniqueItems.json/uniqueItems validation/non-unique array of nested objects is invalid",
165-
"draft6/uniqueItems.json/uniqueItems validation/non-unique array of arrays is invalid",
166-
"draft6/uniqueItems.json/uniqueItems validation/non-unique array of more than two arrays is invalid",
167-
"draft6/uniqueItems.json/uniqueItems validation/non-unique heterogeneous types are invalid",
168-
"draft6/uniqueItems.json/uniqueItems validation/objects are non-unique despite key order",
169157
"draft6/unknownKeyword.json/$id inside an unknown keyword is not a real identifier/type matches second anyOf, which has a real schema in it",
170158
"draft7/boolean_schema.json/boolean schema 'false'/number is invalid",
171159
"draft7/boolean_schema.json/boolean schema 'false'/string is invalid",
@@ -198,12 +186,6 @@ const skippedTests = new Set([
198186
"draft7/refRemote.json/root ref in remote ref/null is valid",
199187
"draft7/refRemote.json/remote ref with ref to definitions/valid",
200188
"draft7/refRemote.json/Location-independent identifier in remote ref/integer is valid",
201-
"draft7/uniqueItems.json/uniqueItems validation/non-unique array of objects is invalid",
202-
"draft7/uniqueItems.json/uniqueItems validation/non-unique array of nested objects is invalid",
203-
"draft7/uniqueItems.json/uniqueItems validation/non-unique array of arrays is invalid",
204-
"draft7/uniqueItems.json/uniqueItems validation/non-unique array of more than two arrays is invalid",
205-
"draft7/uniqueItems.json/uniqueItems validation/non-unique heterogeneous types are invalid",
206-
"draft7/uniqueItems.json/uniqueItems validation/objects are non-unique despite key order",
207189
"draft7/unknownKeyword.json/$id inside an unknown keyword is not a real identifier/type matches second anyOf, which has a real schema in it",
208190
"draft2019-09/anchor.json/Location-independent identifier with absolute URI/match",
209191
"draft2019-09/anchor.json/Location-independent identifier with base URI change in subschema/match",
@@ -275,12 +257,6 @@ const skippedTests = new Set([
275257
"draft2019-09/unevaluatedProperties.json/unevaluatedProperties with $ref/with no unevaluated properties",
276258
"draft2019-09/unevaluatedProperties.json/dynamic evalation inside nested refs/all is valid",
277259
"draft2019-09/unevaluatedProperties.json/dynamic evalation inside nested refs/all + foo is valid",
278-
"draft2019-09/uniqueItems.json/uniqueItems validation/non-unique array of objects is invalid",
279-
"draft2019-09/uniqueItems.json/uniqueItems validation/non-unique array of nested objects is invalid",
280-
"draft2019-09/uniqueItems.json/uniqueItems validation/non-unique array of arrays is invalid",
281-
"draft2019-09/uniqueItems.json/uniqueItems validation/non-unique array of more than two arrays is invalid",
282-
"draft2019-09/uniqueItems.json/uniqueItems validation/non-unique heterogeneous types are invalid",
283-
"draft2019-09/uniqueItems.json/uniqueItems validation/objects are non-unique despite key order",
284260
"draft2019-09/unknownKeyword.json/$id inside an unknown keyword is not a real identifier/type matches second anyOf, which has a real schema in it",
285261
"draft2019-09/vocabulary.json/schema that uses custom metaschema with with no validation vocabulary/no validation: invalid number, but it still validates",
286262
"draft2020-12/anchor.json/Location-independent identifier with absolute URI/match",
@@ -349,12 +325,6 @@ const skippedTests = new Set([
349325
"draft2020-12/unevaluatedProperties.json/unevaluatedProperties with $ref/with no unevaluated properties",
350326
"draft2020-12/unevaluatedProperties.json/dynamic evalation inside nested refs/all is valid",
351327
"draft2020-12/unevaluatedProperties.json/dynamic evalation inside nested refs/all + foo is valid",
352-
"draft2020-12/uniqueItems.json/uniqueItems validation/non-unique array of objects is invalid",
353-
"draft2020-12/uniqueItems.json/uniqueItems validation/non-unique array of nested objects is invalid",
354-
"draft2020-12/uniqueItems.json/uniqueItems validation/non-unique array of arrays is invalid",
355-
"draft2020-12/uniqueItems.json/uniqueItems validation/non-unique array of more than two arrays is invalid",
356-
"draft2020-12/uniqueItems.json/uniqueItems validation/non-unique heterogeneous types are invalid",
357-
"draft2020-12/uniqueItems.json/uniqueItems validation/objects are non-unique despite key order",
358328
"draft2020-12/unknownKeyword.json/$id inside an unknown keyword is not a real identifier/type matches second anyOf, which has a real schema in it",
359329
"draft2020-12/vocabulary.json/schema that uses custom metaschema with with no validation vocabulary/no validation: invalid number, but it still validates"
360330
]);

0 commit comments

Comments
 (0)