in language-service/src/parser/jsonParser.ts [438:510]
public validate(schema: JSONSchema, validationResult: ValidationResult, matchingSchemas: ISchemaCollector): void {
if (!matchingSchemas.include(this)) {
return;
}
super.validate(schema, validationResult, matchingSchemas);
if (Array.isArray(schema.items)) {
let subSchemas = <JSONSchema[]>schema.items;
subSchemas.forEach((subSchema, index) => {
let itemValidationResult = new ValidationResult();
let item = this.items[index];
if (item) {
item.validate(subSchema, itemValidationResult, matchingSchemas);
validationResult.mergePropertyMatch(itemValidationResult);
} else if (this.items.length >= subSchemas.length) {
validationResult.propertiesValueMatches++;
}
});
if (this.items.length > subSchemas.length) {
if (typeof schema.additionalItems === 'object') {
for (let i = subSchemas.length; i < this.items.length; i++) {
let itemValidationResult = new ValidationResult();
this.items[i].validate(<any>schema.additionalItems, itemValidationResult, matchingSchemas);
validationResult.mergePropertyMatch(itemValidationResult);
}
} else if (schema.additionalItems === false) {
validationResult.addProblem({
location: { start: this.start, end: this.end },
severity: ProblemSeverity.Warning,
getMessage: () => localize('additionalItemsWarning', 'Array has too many items according to schema. Expected {0} or fewer.', subSchemas.length)
});
}
}
} else if (schema.items) {
for (let item of this.items) {
let itemValidationResult = new ValidationResult();
item.validate(<JSONSchema>schema.items, itemValidationResult, matchingSchemas);
validationResult.mergePropertyMatch(itemValidationResult);
}
}
if (schema.minItems && this.items.length < schema.minItems) {
validationResult.addProblem({
location: { start: this.start, end: this.end },
severity: ProblemSeverity.Warning,
getMessage: () => localize('minItemsWarning', 'Array has too few items. Expected {0} or more.', schema.minItems)
});
}
if (schema.maxItems && this.items.length > schema.maxItems) {
validationResult.addProblem({
location: { start: this.start, end: this.end },
severity: ProblemSeverity.Warning,
getMessage: () => localize('maxItemsWarning', 'Array has too many items. Expected {0} or fewer.', schema.maxItems)
});
}
if (schema.uniqueItems === true) {
let values = this.items.map((node) => {
return node.getValue();
});
let duplicates = values.some((value, index) => {
return index !== values.lastIndexOf(value);
});
if (duplicates) {
validationResult.addProblem({
location: { start: this.start, end: this.end },
severity: ProblemSeverity.Warning,
getMessage: () => localize('uniqueItemsWarning', 'Array has duplicate items.')
});
}
}
}