Tighten XCUI schema consistency rules

This commit is contained in:
2026-04-05 06:02:40 +08:00
parent ff44b15396
commit 7b49a621cc
3 changed files with 131 additions and 1 deletions

View File

@@ -569,6 +569,10 @@ bool BuildSchemaAttributeDefinition(
Containers::Array<UIDocumentDiagnostic>& diagnostics,
Containers::String& inOutErrorMessage) {
bool valid = true;
bool hasExplicitDocumentKind = false;
bool hasExplicitRestrictDocumentKind = false;
bool hasExplicitAllowedValues = false;
if (!node.children.Empty()) {
AppendDiagnostic(
sourcePath,
@@ -640,6 +644,7 @@ bool BuildSchemaAttributeDefinition(
const UIDocumentAttribute* documentKindAttribute =
FindAttributeByName(node, "documentKind", "kind");
if (documentKindAttribute != nullptr && !documentKindAttribute->value.Trim().Empty()) {
hasExplicitDocumentKind = true;
if (!TryParseDocumentKindValueExtended(documentKindAttribute->value, outDefinition.documentKind)) {
AppendDiagnostic(
sourcePath,
@@ -653,11 +658,23 @@ bool BuildSchemaAttributeDefinition(
valid = false;
} else if (outDefinition.valueType == UISchemaValueType::Document) {
outDefinition.restrictDocumentKind = true;
} else {
AppendDiagnostic(
sourcePath,
diagnostics,
inOutErrorMessage,
UIDocumentDiagnosticSeverity::Error,
node.location,
Containers::String("Schema attribute '") +
outDefinition.name +
"' can only declare 'documentKind' for 'document' value types.");
valid = false;
}
}
if (const UIDocumentAttribute* restrictDocumentKindAttribute = FindAttributeByName(node, "restrictDocumentKind");
restrictDocumentKindAttribute != nullptr && !restrictDocumentKindAttribute->value.Trim().Empty()) {
hasExplicitRestrictDocumentKind = true;
if (!TryParseBooleanValueExtended(
restrictDocumentKindAttribute->value,
outDefinition.restrictDocumentKind)) {
@@ -671,14 +688,39 @@ bool BuildSchemaAttributeDefinition(
restrictDocumentKindAttribute->value +
"' for schema attribute 'restrictDocumentKind'.");
valid = false;
} else if (outDefinition.valueType != UISchemaValueType::Document) {
AppendDiagnostic(
sourcePath,
diagnostics,
inOutErrorMessage,
UIDocumentDiagnosticSeverity::Error,
node.location,
Containers::String("Schema attribute '") +
outDefinition.name +
"' can only declare 'restrictDocumentKind' for 'document' value types.");
valid = false;
}
}
if (const UIDocumentAttribute* allowedValuesAttribute = FindAttributeByName(node, "allowedValues", "values");
allowedValuesAttribute != nullptr) {
hasExplicitAllowedValues = true;
outDefinition.allowedValues = ParseAllowedValueList(allowedValuesAttribute->value);
}
if (hasExplicitAllowedValues && outDefinition.valueType != UISchemaValueType::Enum) {
AppendDiagnostic(
sourcePath,
diagnostics,
inOutErrorMessage,
UIDocumentDiagnosticSeverity::Error,
node.location,
Containers::String("Schema attribute '") +
outDefinition.name +
"' can only declare 'allowedValues' for 'enum' value types.");
valid = false;
}
if (outDefinition.valueType == UISchemaValueType::Enum && outDefinition.allowedValues.Empty()) {
AppendDiagnostic(
sourcePath,
@@ -692,6 +734,21 @@ bool BuildSchemaAttributeDefinition(
valid = false;
}
if (outDefinition.valueType == UISchemaValueType::Document &&
outDefinition.restrictDocumentKind &&
!hasExplicitDocumentKind) {
AppendDiagnostic(
sourcePath,
diagnostics,
inOutErrorMessage,
UIDocumentDiagnosticSeverity::Error,
node.location,
Containers::String("Schema attribute '") +
outDefinition.name +
"' must declare 'documentKind' when 'restrictDocumentKind' is true.");
valid = false;
}
return valid;
}