@fragment-dev/cli
Version:
182 lines (179 loc) • 5.46 kB
JavaScript
import {
extractSchemaMetadata,
isJsonParseError,
validateSchemaStructure
} from "./chunk-WK54RI4J.js";
import {
DEFAULT_SCHEMA_PATH
} from "./chunk-A4BSWX5D.js";
import {
logValidationErrors
} from "./chunk-AY76YKC7.js";
import {
FragmentCommand,
require_lib
} from "./chunk-UD22EIAP.js";
import {
colorizeJson,
emphasize,
getParsedSchema,
getParsedSchemaString,
parseJSON
} from "./chunk-UDU5PBTV.js";
import {
require_source
} from "./chunk-M5OAS5QZ.js";
import {
__toESM,
init_cjs_shims
} from "./chunk-7GH3YGSC.js";
// src/commands/verify-schema.ts
init_cjs_shims();
var import_core = __toESM(require_lib(), 1);
var import_chalk = __toESM(require_source(), 1);
var VerifySchema = class _VerifySchema extends FragmentCommand {
static {
this.description = "Verify that a schema file follows the Fragment JSON schema format";
}
static {
this.examples = [
"<%= config.bin %> <%= command.id %>",
"<%= config.bin %> <%= command.id %> --path ./my-schema.jsonc",
"<%= config.bin %> <%= command.id %> -p fragment/fragment.json --verbose",
"<%= config.bin %> <%= command.id %> --path ./my-schema.jsonc --json"
];
}
static {
this.flags = {
path: import_core.Flags.string({
char: "p",
description: "Path to schema file (e.g. my-schema.jsonc)",
default: DEFAULT_SCHEMA_PATH
}),
verbose: import_core.Flags.boolean({
char: "v",
description: "Show detailed validation output",
default: false
}),
json: import_core.Flags.boolean({
char: "j",
description: "Output validation results as JSON for programmatic use",
default: false
})
};
}
async run() {
const { flags } = await this.parse(_VerifySchema);
try {
let schema;
if (flags.json) {
const schemaString = getParsedSchemaString({
command: this,
schemaPath: flags.path,
silenceNonErrorLogging: true,
requireEnvVariables: false
});
const [parsedSchema, parseError] = parseJSON(schemaString);
if (parseError) {
const error = new Error(`Failed to parse ${flags.path}: ${parseError.message}`);
error.isParseError = true;
throw error;
}
schema = parsedSchema;
} else {
schema = getParsedSchema({
command: this,
schemaPath: flags.path,
silenceNonErrorLogging: !flags.verbose
});
}
this.validateSchema(schema);
if (flags.json) {
this.outputJsonResult(schema, flags.path, true);
} else {
if (flags.verbose) {
this.displayParsedSchema(schema);
this.displayValidationDetails(schema);
}
this.displaySuccessMessage(schema, flags.verbose);
}
} catch (error) {
if (flags.json) {
this.outputJsonResult(null, flags.path, false, error);
} else {
this.handleValidationError(error, flags.path);
}
}
}
displayParsedSchema(schema) {
this.log("\n\u{1F4CB} Parsed schema:");
this.log(colorizeJson(JSON.stringify(schema, null, 2)));
this.log("\n");
}
validateSchema(schema) {
validateSchemaStructure(schema);
}
displayValidationDetails(schema) {
const metadata = extractSchemaMetadata(schema);
this.log("\u2705 Schema structure validation passed");
this.log(` Schema key: ${emphasize(metadata.key)}`);
this.log(` Schema name: ${emphasize(metadata.name)}`);
this.log(` Accounts: ${emphasize(metadata.accountCount.toString())}`);
this.log(` Ledger entry types: ${emphasize(metadata.entryTypeCount.toString())}`);
}
displaySuccessMessage(schema, verbose) {
this.log(`
${import_chalk.default.green("\u2705 Schema validation successful!")}`);
this.log(`Schema "${schema.key}" is valid and follows the Fragment JSON schema format.`);
if (!verbose) {
this.log(`
Run with ${emphasize("--verbose")} flag to see detailed validation output.`);
}
}
outputJsonResult(schema, schemaPath, success, error) {
const result = {
success,
schemaPath,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
};
if (success && schema) {
const metadata = extractSchemaMetadata(schema);
result.schema = {
key: metadata.key,
name: metadata.name,
accountCount: metadata.accountCount,
entryTypeCount: metadata.entryTypeCount
};
}
if (!success && error) {
result.error = {
message: error.message || "Unknown validation error",
type: isJsonParseError(error) ? "parse_error" : "validation_error"
};
if (error.validationErrors) {
result.error.validationErrors = error.validationErrors;
}
}
this.log(JSON.stringify(result, null, 2));
if (!success) {
this.exit(1);
}
}
handleValidationError(error, schemaPath) {
if (!isJsonParseError(error)) {
this.logToStderr(`
${import_chalk.default.red("\u274C Schema validation failed!")}`);
if (error.validationErrors) {
logValidationErrors(this, error.validationErrors);
} else {
this.logToStderr(import_chalk.default.red("Error:") + " " + (error.message || "Unknown validation error"));
}
this.logToStderr(`
${import_chalk.default.gray("Schema file:")} ${schemaPath}`);
}
this.exit(1);
}
};
export {
VerifySchema
};