UNPKG

@nosana/schema-validator

Version:
243 lines (239 loc) 9.78 kB
import AJV from 'ajv'; import { parse } from 'yaml'; export { parse as parseYaml, stringify as stringifyYaml } from 'yaml'; const NosPipelineSchema = { type: "object", properties: { global: { type: "object", nullable: false, properties: { trigger: { type: "object", nullable: true, properties: { branch: { type: ["array", "string"], items: { type: "string" }, nullable: true, uniqueItems: true, }, }, }, secrets: { type: "array", items: { type: "string" }, nullable: true, uniqueItems: true, }, environment: { anyOf: [ { type: "object", nullable: true, additionalProperties: { type: "string" }, }, { type: "object", nullable: true, properties: { type: { type: "string", nullable: false, }, endpoint: { type: "string", nullable: false, }, value: { type: "string", nullable: false, }, }, }, ], }, allow_failure: { type: "boolean", nullable: true, }, }, }, jobs: { type: "array", nullable: false, items: { type: "object", nullable: false, properties: { name: { type: "string", nullable: false, }, image: { type: "string", nullable: true, minLength: 1, maxLength: 4096, }, secrets: { type: "array", items: { type: "string" }, nullable: true, uniqueItems: true, }, environment: { anyOf: [ { type: "object", nullable: true, additionalProperties: { type: "string" }, }, { type: "object", nullable: true, properties: { type: { type: "string", nullable: false, }, endpoint: { type: "string", nullable: false, }, value: { type: "string", nullable: false, }, }, }, ], }, allow_failure: { type: "boolean", nullable: true, }, resources: { type: "array", nullable: true, items: { type: "object", nullable: true, properties: { name: { type: "string", nullable: false, }, path: { type: "string", nullable: true, }, }, }, }, artifacts: { type: "array", nullable: true, items: { type: "object", nullable: true, properties: { name: { type: "string", nullable: false, }, path: { type: "string", nullable: true, }, }, }, }, commands: { type: "array", nullable: false, additionalItems: false, items: { anyOf: [ { type: "string", nullable: false, }, { type: "object", nullable: false, properties: { cmd: { type: "string", nullable: false, }, working_dir: { type: "string", nullable: true, }, allow_failure: { type: "boolean", nullable: true, }, resources: { type: "array", nullable: true, items: { type: "object", nullable: true, properties: { name: { type: "string", nullable: false, }, path: { type: "string", nullable: true, }, }, }, }, artifacts: { type: "array", nullable: true, items: { type: "object", nullable: true, properties: { name: { type: "string", nullable: false, }, path: { type: "string", nullable: false, }, }, }, }, }, }, ], }, }, }, }, }, }, required: ["global", "jobs"], additionalProperties: false, }; const ajv = new AJV({ allErrors: true, verbose: true, allowUnionTypes: true, strict: false, }); const validateJson = (schema) => { const parsed = JSON.parse(schema); const validate = ajv.compile(NosPipelineSchema); const valid = validate(parsed); return { valid, ...validate }; }; const validateYaml = (yaml) => validateJson(JSON.stringify(parse(yaml))); export { NosPipelineSchema, validateJson, validateYaml };