fix: update the validation of schema reference

This commit is contained in:
Eric Chen 2020-04-30 13:51:08 +08:00 коммит произвёл GitHub
Родитель 545867baba
Коммит 86bf8b6833
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 29 добавлений и 7 удалений

Просмотреть файл

@ -45,7 +45,9 @@ export class DigitalTwinConstants {
public static readonly ID = "@id";
public static readonly TYPE = "@type";
public static readonly CONTEXT = "@context";
public static readonly NAME = "name";
public static readonly NAME_PROPERTY = "name";
public static readonly UNIT_PROPERTY = "unit";
public static readonly SCHEMA_CLASS = "Schema";
public static readonly LINE_FEED = "\n";
public static readonly DEFAULT_DELIMITER = ",";
public static readonly SCHEMA_DELIMITER = "#";
@ -54,5 +56,7 @@ export class DigitalTwinConstants {
public static readonly REQUIRED_PROPERTY_LABEL = "(required)";
public static readonly WORD_STOP = ' \t\n\r\v":{[,';
public static readonly CONTEXT_REGEX = /^dtmi:dtdl:context;(\d+)$/;
public static readonly DTMI_REGEX = /^dtmi:[A-Za-z](?:[A-Za-z0-9_]*[A-Za-z0-9])?(?::[A-Za-z](?:[A-Za-z0-9_]*[A-Za-z0-9])?)*;[1-9][0-9]{0,8}$/;
public static readonly DTMI_REGEX = new RegExp(
"^dtmi:[A-Za-z](?:[A-Za-z0-9_]*[A-Za-z0-9])?(?::[A-Za-z](?:[A-Za-z0-9_]*[A-Za-z0-9])?)*;[1-9][0-9]{0,8}$",
);
}

Просмотреть файл

@ -3,7 +3,6 @@
import * as parser from "jsonc-parser";
import * as vscode from "vscode";
import { Constants } from "../common/constants";
import { DiagnosticMessage, DigitalTwinConstants } from "./digitalTwinConstants";
import { ClassNode, Literal, PropertyNode } from "./digitalTwinGraph";
import { IntelliSenseUtility, JsonNodeType, ModelContent, PropertyPair } from "./intelliSenseUtility";
@ -258,9 +257,14 @@ export class DigitalTwinDiagnosticProvider {
if (!isPartitionClass) {
DigitalTwinDiagnosticProvider.addProblemOfUnexpectedProperty(propertyPair.name, problems);
}
break;
case DigitalTwinConstants.TYPE:
// skip since @type has been validated
break;
case DigitalTwinConstants.UNIT_PROPERTY:
// TODO: remove this logic when supporting semantic type
// add this special logic here is is to not show diagnostic error for unit property
break;
default:
// validate property is expected
propertyNode = expectProperties.get(propertyName);
@ -382,13 +386,14 @@ export class DigitalTwinDiagnosticProvider {
* @param problems problem collection
*/
private static validateIRINode(jsonNode: parser.Node, digitalTwinNode: PropertyNode, problems: Problem[]): void {
// constraint is prior to type, e.g. valueSchema
// constraint is prior to type, e.g. Enum/valueSchema
let instances: string[];
if (digitalTwinNode.constraint.in) {
instances = digitalTwinNode.constraint.in.map((id) => IntelliSenseUtility.resolveNodeName(id));
DigitalTwinDiagnosticProvider.validateInstances(jsonNode, instances, problems);
return;
}
// e.g. Relationship/target
if (!digitalTwinNode.type) {
DigitalTwinDiagnosticProvider.validateDtmi(jsonNode, problems);
return;
@ -397,6 +402,10 @@ export class DigitalTwinDiagnosticProvider {
if (!classNode) {
return;
}
// validate value is a reference to the element of Interface/schemas, e.g. Telmetry/schema
if (DigitalTwinDiagnosticProvider.isSchemaReference(jsonNode, classNode)) {
return;
}
// validate instance
instances = IntelliSenseUtility.getInstancesOfClassNode(classNode);
if (!instances.length) {
@ -432,12 +441,21 @@ export class DigitalTwinDiagnosticProvider {
DigitalTwinDiagnosticProvider.addProblem(jsonNode, problems, DiagnosticMessage.InvalidDtmiLength);
return;
}
const regex = new RegExp(DigitalTwinConstants.DTMI_REGEX);
if (!regex.test(id)) {
if (!DigitalTwinConstants.DTMI_REGEX.test(id)) {
DigitalTwinDiagnosticProvider.addProblem(jsonNode, problems, DiagnosticMessage.InvalidDtmiPattern);
}
}
/**
* check if class node is a reference to schema
* @param jsonNode json node
* @param classNode class node
*/
private static isSchemaReference(jsonNode: parser.Node, classNode: ClassNode): boolean {
const id: string = jsonNode.value as string;
return classNode.name === DigitalTwinConstants.SCHEMA_CLASS && DigitalTwinConstants.DTMI_REGEX.test(id);
}
/**
* validate json number node
* @param jsonNode json node

Просмотреть файл

@ -247,7 +247,7 @@ export class IntelliSenseUtility {
if (!propertyPair) {
continue;
}
if (propertyPair.name.value === DigitalTwinConstants.NAME) {
if (propertyPair.name.value === DigitalTwinConstants.NAME_PROPERTY) {
return propertyPair.value;
}
}