11#!/usr/bin/env node
22
33import { readFile } from "node:fs/promises" ;
4- import yaml from "yaml" ;
4+ import YAML from "yaml" ;
55import { setMetaSchemaOutputFormat , validate } from "@hyperjump/json-schema/openapi-3-1" ;
66import { BASIC } from "@hyperjump/json-schema/experimental" ;
77
8+ import contentTypeParser from "content-type" ;
9+ import { addMediaTypePlugin } from "@hyperjump/browser" ;
10+ import { buildSchemaDocument } from "@hyperjump/json-schema/experimental" ;
11+
12+ addMediaTypePlugin ( "application/schema+yaml" , {
13+ parse : async ( response ) => {
14+ const contentType = contentTypeParser . parse ( response . headers . get ( "content-type" ) ?? "" ) ;
15+ const contextDialectId = contentType . parameters . schema ?? contentType . parameters . profile ;
16+
17+ const foo = YAML . parse ( await response . text ( ) ) ;
18+ return buildSchemaDocument ( foo , response . url , contextDialectId ) ;
19+ } ,
20+ fileMatcher : ( path ) => path . endsWith ( ".yaml" )
21+ } ) ;
822
923const defaultOutputFormat = BASIC ;
1024
@@ -29,10 +43,10 @@ const outputFormat = args.format || defaultOutputFormat;
2943setMetaSchemaOutputFormat ( outputFormat ) ;
3044
3145// Compile / meta-validate
32- const validateOpenApi = await validate ( `./schemas/v3.1/${ schemaType } .json ` ) ;
46+ const validateOpenApi = await validate ( `./schemas/v3.1/${ schemaType } .yaml ` ) ;
3347
3448// Validate instance
3549const instanceYaml = await readFile ( `${ process . argv [ process . argv . length - 1 ] } ` , "utf8" ) ;
36- const instance = yaml . parse ( instanceYaml ) ;
50+ const instance = YAML . parse ( instanceYaml ) ;
3751const results = validateOpenApi ( instance , outputFormat ) ;
3852console . log ( JSON . stringify ( results , null , " " ) ) ;
0 commit comments