Path: blob/main/src/command/dev-call/validate-yaml/cmd.ts
3589 views
/*1* cmd.ts2*3* Copyright (C) 2025 Posit Software, PBC4*/56import { Command } from "cliffy/command/mod.ts";7import { initYamlIntelligenceResourcesFromFilesystem } from "../../../core/schema/utils.ts";8import { readAndValidateYamlFromMappedString } from "../../../core/lib/yaml-schema/validated-yaml.ts";9import { mappedStringFromFile } from "../../../core/mapped-text.ts";10import {11getSchemaDefinition,12setSchemaDefinition,13} from "../../../core/lib/yaml-validation/schema.ts";14import { error } from "../../../deno_ral/log.ts";15import { tidyverseFormatError } from "../../../core/lib/errors.ts";16import {17convertFromYaml,18getSchemaSchemas,19} from "../../../core/lib/yaml-schema/from-yaml.ts";2021const getSchema = async (schemaNameOrFile: string) => {22if (schemaNameOrFile.endsWith(".yml")) {23getSchemaSchemas();24// it's a file, we load it, validate it against the schema schema25// and then return it26const file = mappedStringFromFile(schemaNameOrFile);27const schema = getSchemaDefinition("schema/schema");28const result = await readAndValidateYamlFromMappedString(29file,30schema,31);32if (result.yamlValidationErrors.length) {33error("Schema file is not valid");34for (const err of result.yamlValidationErrors) {35error(tidyverseFormatError(err.niceError), { colorize: false });36}37Deno.exit(1);38}39const schemaName = `user-schema-${schemaNameOrFile}`;40const newSchema = convertFromYaml(result.yaml);41newSchema.$id = schemaName;42setSchemaDefinition(newSchema);43return getSchemaDefinition(schemaName);44} else {45// it's a schema name, we get it from the schema registry46// and return it47return getSchemaDefinition(schemaNameOrFile);48}49};5051export const validateYamlCommand = new Command()52.name("validate-yaml")53.hidden()54.arguments("<input:string>")55.option(56"-s, --schema [schema:string]",57"Name of schema in Quarto's definitions.yml. If string ends with .yml, it is treated as a file name for a new schema, which is validated, loaded, and then used.",58)59.option(60"--json",61"If set, output error messages in JSON format.",62)63.description(64"Validates a YAML file against Quarto's schemas.\n\n",65)66.action(async (options: any, input: string) => {67await initYamlIntelligenceResourcesFromFilesystem();68if (!options.schema) {69throw new Error("Schema name or file is required");70}71const file = mappedStringFromFile(input);72const schema = await getSchema(options.schema);73const result = await readAndValidateYamlFromMappedString(74file,75schema,76);77if (options.json) {78console.log(JSON.stringify(result.yamlValidationErrors, null, 2));79} else {80for (const err of result.yamlValidationErrors) {81error(tidyverseFormatError(err.niceError), { colorize: false });82}83}84if (result.yamlValidationErrors.length) {85Deno.exit(1);86}87});888990