@@ -2,16 +2,16 @@ import * as YAML from "yaml";
22
33import { schemaYamls } from "./schemas/yamls" ;
44
5- import type { JSONSchema as JSONSchemaTyped } from "json-schema-typed/draft-2020-12"
5+ import type { JSONSchema as JSONSchemaTyped } from "json-schema-typed/draft-2020-12" ;
66
77export type JSONSchema = Exclude < JSONSchemaTyped , boolean > ;
88
99export interface DescribeSchemaOptions <
10- S extends SchemaReference = SchemaReference
10+ S extends SchemaReference = SchemaReference ,
1111> {
1212 schema : S ;
1313 pointer ?: SchemaPointer ;
14- } ;
14+ }
1515
1616export interface SchemaInfo {
1717 id ?: string ; // root ID only
@@ -23,46 +23,42 @@ export interface SchemaInfo {
2323
2424const parseOptions = {
2525 // merge keys were removed from YAML 1.2 spec but used by these schemas
26- merge : true
26+ merge : true ,
2727} ;
2828
2929export function describeSchema ( {
3030 schema,
31- pointer
31+ pointer,
3232} : DescribeSchemaOptions ) : SchemaInfo {
3333 if ( typeof pointer === "string" && ! pointer . startsWith ( "#" ) ) {
3434 throw new Error ( "`pointer` option must start with '#'" ) ;
3535 }
3636
37- const pointerOptions = pointer
38- ? { pointer }
39- : { } ;
37+ const pointerOptions = pointer ? { pointer } : { } ;
4038
4139 if ( referencesId ( schema ) ) {
4240 return describeSchemaById ( {
43- schema : typeof schema === "object"
44- ? schema
45- : { id : schema } ,
46- ...pointerOptions
41+ schema : typeof schema === "object" ? schema : { id : schema } ,
42+ ...pointerOptions ,
4743 } ) ;
4844 }
4945
5046 if ( referencesYaml ( schema ) ) {
5147 return describeSchemaByYaml ( {
5248 schema,
53- ...pointerOptions
49+ ...pointerOptions ,
5450 } ) ;
5551 }
5652
5753 return describeSchemaByObject ( {
5854 schema,
59- ...pointerOptions
55+ ...pointerOptions ,
6056 } ) ;
6157}
6258
6359function describeSchemaById ( {
6460 schema : { id : referencedId } ,
65- pointer : relativePointer
61+ pointer : relativePointer ,
6662} : DescribeSchemaOptions < SchemaById > ) : SchemaInfo {
6763 // we need to handle the case where the schema is referenced by an ID
6864 // with a pointer specified, possibly with a separate `pointer` field too
@@ -72,7 +68,7 @@ function describeSchemaById({
7268 ? joinSchemaPointers ( [ `#${ rawReferencedPointer } ` , relativePointer ] )
7369 : relativePointer ;
7470
75- const rootYaml = schemaYamls [ id ]
71+ const rootYaml = schemaYamls [ id ] ;
7672 if ( ! rootYaml ) {
7773 throw new Error ( `Unknown schema with $id "${ id } "` ) ;
7874 }
@@ -87,13 +83,13 @@ function describeSchemaById({
8783 ...( pointer ? { pointer } : { } ) ,
8884 yaml,
8985 schema,
90- rootSchema
91- }
86+ rootSchema,
87+ } ;
9288}
9389
9490function describeSchemaByYaml ( {
9591 schema : { yaml : referencedYaml } ,
96- pointer
92+ pointer,
9793} : DescribeSchemaOptions < SchemaByYaml > ) : SchemaInfo {
9894 const yaml = pointToYaml ( referencedYaml , pointer ) ;
9995
@@ -108,21 +104,21 @@ function describeSchemaByYaml({
108104 ...( pointer ? { pointer } : { } ) ,
109105 yaml,
110106 schema,
111- rootSchema
112- }
107+ rootSchema,
108+ } ;
113109 } else {
114110 return {
115111 ...( pointer ? { pointer } : { } ) ,
116112 yaml,
117113 schema,
118- rootSchema
119- }
114+ rootSchema,
115+ } ;
120116 }
121117}
122118
123119function describeSchemaByObject ( {
124120 schema : rootSchema ,
125- pointer
121+ pointer,
126122} : DescribeSchemaOptions < object > ) : SchemaInfo {
127123 const rootYaml = YAML . stringify ( rootSchema ) ;
128124
@@ -138,24 +134,24 @@ function describeSchemaByObject({
138134 ...( pointer ? { pointer } : { } ) ,
139135 yaml,
140136 schema,
141- rootSchema
142- }
137+ rootSchema,
138+ } ;
143139 } else {
144140 return {
145141 ...( pointer ? { pointer } : { } ) ,
146142 yaml,
147143 schema,
148- rootSchema
149- }
144+ rootSchema,
145+ } ;
150146 }
151147}
152148
153149function joinSchemaPointers (
154- pointers : ( SchemaPointer | undefined ) [ ]
150+ pointers : ( SchemaPointer | undefined ) [ ] ,
155151) : SchemaPointer | undefined {
156152 const joined = pointers
157153 . filter ( ( pointer ) : pointer is SchemaPointer => typeof pointer === "string" )
158- . map ( pointer => pointer . slice ( 1 ) )
154+ . map ( ( pointer ) => pointer . slice ( 1 ) )
159155 . join ( "" ) ;
160156
161157 if ( joined . length === 0 ) {
@@ -165,10 +161,7 @@ function joinSchemaPointers(
165161 return `#${ joined } ` ;
166162}
167163
168- function pointToYaml (
169- yaml : string ,
170- pointer ?: SchemaPointer
171- ) : string {
164+ function pointToYaml ( yaml : string , pointer ?: SchemaPointer ) : string {
172165 if ( ! pointer ) {
173166 return yaml ;
174167 }
@@ -192,9 +185,8 @@ type Impossible<K extends keyof any> = {
192185 [ P in K ] : never ;
193186} ;
194187
195- type NoExtraProperties < T , U extends T = T > =
196- & U
197- & Impossible < Exclude < keyof U , keyof T > > ;
188+ type NoExtraProperties < T , U extends T = T > = U &
189+ Impossible < Exclude < keyof U , keyof T > > ;
198190
199191export type SchemaPointer = `#${string } `;
200192
@@ -215,7 +207,7 @@ export type SchemaByYaml = NoExtraProperties<{
215207} > ;
216208
217209export function referencesId (
218- schema : SchemaReference
210+ schema : SchemaReference ,
219211) : schema is SchemaId | SchemaById {
220212 return (
221213 typeof schema === "string" ||
@@ -224,8 +216,11 @@ export function referencesId(
224216}
225217
226218export function referencesYaml (
227- schema : SchemaReference
219+ schema : SchemaReference ,
228220) : schema is SchemaByYaml {
229- return typeof schema === "object" &&
230- Object . keys ( schema ) . length === 1 && "yaml" in schema ;
221+ return (
222+ typeof schema === "object" &&
223+ Object . keys ( schema ) . length === 1 &&
224+ "yaml" in schema
225+ ) ;
231226}
0 commit comments