Skip to content

Commit e73dd8b

Browse files
committed
Use ajv for validating data schemas
1 parent f432dd6 commit e73dd8b

4 files changed

Lines changed: 428 additions & 98 deletions

File tree

client/src/scg.ts

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as yaml from 'js-yaml';
22
import * as path from 'path';
33
import * as vscode from 'vscode';
4+
import Ajv from 'ajv';
5+
6+
import * as scgSchema from '../../schemas/scg_config.schema.json';
7+
8+
const ajv = new Ajv();
9+
const validate_scg = ajv.compile<ScgConfigSchema>(scgSchema);
410

511
export interface ScgConfigSchema {
612
outputfile?: string;
@@ -153,90 +159,29 @@ export async function readScgConfig(filePath: string): Promise<ScgConfigSchema |
153159
const uri = vscode.Uri.file(filePath);
154160
const document = await vscode.workspace.openTextDocument(uri);
155161
const fileContents = document.getText();
156-
const data = yaml.load(fileContents) as ScgConfigSchema;
157-
if (!validateScgConfig(data)) {
158-
return undefined;
162+
const data = yaml.load(fileContents);
163+
164+
if (validate_scg(data)) {
165+
return data;
159166
}
160-
return data;
167+
return undefined;
161168
} catch {
162169
return undefined;
163170
}
164171
}
165172

166-
export function validateScgConfig(data: ScgConfigSchema): boolean {
167-
if (!data.templatepath || typeof data.templatepath !== 'string') return false;
168-
if (data.adjustspacing && typeof data.adjustspacing !== 'boolean') return false;
169-
if (data.verifycontent && typeof data.verifycontent !== 'boolean') return false;
170-
171-
// Validate optional fields
172-
if (data.outputfile && typeof data.outputfile !== 'string') return false;
173-
174-
// Validate counters
175-
if (data.counters) {
176-
for (const counter of data.counters) {
177-
if (typeof counter.name !== 'string') return false;
178-
if (counter.value && typeof counter.value !== 'number') return false;
179-
}
180-
}
181-
182-
// Validate sources
183-
for (const source of data.sources) {
184-
if (typeof source.filename !== 'string' || typeof source.id !== 'string') return false;
185-
if (source.sheet && typeof source.sheet !== 'string') return false;
186-
if (source.delimiter && typeof source.delimiter !== 'string') return false;
187-
}
188-
189-
// Validate layout
190-
for (const layout of data.layout) {
191-
if (typeof layout.name !== 'string') return false;
192-
if (layout.source && typeof layout.source !== 'string') return false;
193-
if (layout.include && typeof layout.include !== 'object') return false;
194-
}
195-
196-
return true;
197-
}
198-
199173
export async function isScgConfig(filePath: string): Promise<boolean> {
200174
try {
201175
const uri = vscode.Uri.file(filePath);
202176
const document = await vscode.workspace.openTextDocument(uri);
203177
const fileContents = document.getText();
204-
const data = yaml.load(fileContents) as ScgConfigSchema;
205-
return validateScgConfig(data);
178+
const data = yaml.load(fileContents);
179+
return validate_scg(data);
206180
} catch {
207181
return false;
208182
}
209183
}
210184

211-
export async function findScgConfigFiles(): Promise<vscode.Uri[]> {
212-
try {
213-
const activeEditor = vscode.window.activeTextEditor;
214-
if (!activeEditor) {
215-
return [];
216-
}
217-
let currentFolder = vscode.Uri.file(activeEditor.document.uri.fsPath).with({ path: vscode.Uri.file(activeEditor.document.uri.fsPath).path.replace(/\/[^/]+$/, '') });
218-
const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
219-
220-
while (currentFolder.fsPath.startsWith(workspaceFolder || '') && currentFolder.fsPath !== workspaceFolder) {
221-
const files = await vscode.workspace.findFiles(new vscode.RelativePattern(currentFolder, '*.yaml'));
222-
const scgConfigFiles: vscode.Uri[] = [];
223-
for (const file of files) {
224-
if (await isScgConfig(file.fsPath)) {
225-
scgConfigFiles.push(file);
226-
}
227-
}
228-
if (scgConfigFiles.length > 0) {
229-
return scgConfigFiles;
230-
}
231-
currentFolder = vscode.Uri.file(currentFolder.fsPath).with({ path: vscode.Uri.file(currentFolder.fsPath).path.replace(/\/[^/]+$/, '') });
232-
}
233-
return [];
234-
} catch (error) {
235-
console.error('Error finding SCG config files:', error);
236-
return [];
237-
}
238-
}
239-
240185

241186
export class ScgSource {
242187
id: string;

0 commit comments

Comments
 (0)