-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ChildrenRequiredPropertyValidationRule
For ensuring required properties exist in children and optionaly have a value.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...es/server-common/src/providers/validation/rules/ChildrenRequiredPropertyValidationRule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { DocumentContext, ValidationRule } from "@gxwf/server-common/src/languageTypes"; | ||
import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver-types"; | ||
|
||
/** | ||
* Validation rule to check that all children of a node have a required property. | ||
*/ | ||
export class ChildrenRequiredPropertyValidationRule implements ValidationRule { | ||
constructor( | ||
private nodePath: string, | ||
private propertyName: string, | ||
private valueRequired: boolean = true, | ||
private severity: DiagnosticSeverity = DiagnosticSeverity.Error, | ||
private message?: string | ||
) {} | ||
|
||
public async validate(documentContext: DocumentContext): Promise<Diagnostic[]> { | ||
const result: Diagnostic[] = []; | ||
const targetNode = documentContext.nodeManager.getNodeFromPath(this.nodePath); | ||
if (!targetNode) { | ||
// The target node does not exist, so we can't check for children. | ||
return Promise.resolve(result); | ||
} | ||
|
||
const children = documentContext.nodeManager.getChildren(targetNode); | ||
for (const child of children) { | ||
if (child.type !== "object") { | ||
continue; | ||
} | ||
for (const childProperty of child.properties) { | ||
const targetChildNode = documentContext.nodeManager.getPropertyNodeByName(childProperty, this.propertyName); | ||
if (!targetChildNode) { | ||
result.push({ | ||
message: this.message ?? `Missing required property "${this.propertyName}".`, | ||
range: documentContext.nodeManager.getNodeRange(childProperty.keyNode), | ||
severity: this.severity, | ||
}); | ||
} | ||
|
||
if (this.valueRequired && targetChildNode) { | ||
const missingValue = documentContext.nodeManager.isNodeEmpty(targetChildNode); | ||
if (missingValue) { | ||
result.push({ | ||
message: `Missing required value in property "${this.propertyName}".`, | ||
range: documentContext.nodeManager.getNodeRange(targetChildNode), | ||
severity: this.severity, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
return Promise.resolve(result); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
server/packages/server-common/src/providers/validation/rules/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters