@@ -4,14 +4,14 @@ import { Draft04, type Draft, type JsonError } from "json-schema-library";
44
55import { getJSONSchema , schemaStateField } from "./state.js" ;
66import { joinWithOr } from "./utils/formatting.js" ;
7- import { JSONMode , JSONPointerData } from "./types.js" ;
7+ import { JSONMode , JSONPointerData , RequiredPick } from "./types.js" ;
88import { parseJSONDocumentState } from "./utils/parseJSONDocument.js" ;
9- import { RequiredPick } from "./types.js" ;
109import { el } from "./utils/dom.js" ;
1110import { renderMarkdown } from "./utils/markdown.js" ;
1211import { MODES } from "./constants.js" ;
1312import { parseYAMLDocumentState } from "./utils/parse-yaml-document.js" ;
1413import { parseJSON5DocumentState } from "./utils/parseJSON5Document.js" ;
14+ import { debug } from "./utils/debug.js" ;
1515
1616const getDefaultParser = ( mode : JSONMode ) : typeof parseJSONDocumentState => {
1717 switch ( mode ) {
@@ -96,7 +96,7 @@ export class JSONValidation {
9696 const errors = errorData ?. errors as string [ ] ;
9797 if ( error . code === "one-of-error" && errors ?. length ) {
9898 return `Expected one of ${ joinWithOr (
99- errors as string [ ] ,
99+ errors ,
100100 ( data ) => data . data . expected
101101 ) } `;
102102 }
@@ -110,6 +110,7 @@ export class JSONValidation {
110110 const message = error . message
111111 // don't mention root object
112112 . replaceAll ( "in `#` " , "" )
113+ . replaceAll ( "at `#`" , "" )
113114 . replaceAll ( "/" , "." )
114115 . replaceAll ( "#." , "" ) ;
115116 return message ;
@@ -135,9 +136,10 @@ export class JSONValidation {
135136 try {
136137 errors = this . schema . validate ( json . data ) ;
137138 } catch { }
139+ debug . log ( "xxx" , "validation errors" , errors , json . data ) ;
138140 if ( ! errors . length ) return [ ] ;
139141 // reduce() because we want to filter out errors that don't have a pointer
140- return errors . reduce ( ( acc , error ) => {
142+ return errors . reduce < Diagnostic [ ] > ( ( acc , error ) => {
141143 const pushRoot = ( ) => {
142144 const errorString = this . rewriteError ( error ) ;
143145 acc . push ( {
@@ -156,12 +158,11 @@ export class JSONValidation {
156158 const errorPath = getErrorPath ( error ) ;
157159 const pointer = json . pointers . get ( errorPath ) as JSONPointerData ;
158160 if (
159- error . name === "MaxPropertiesError" ??
161+ error . name === "MaxPropertiesError" ||
160162 error . name === "MinPropertiesError"
161163 ) {
162164 pushRoot ( ) ;
163- }
164- if ( pointer ) {
165+ } else if ( pointer ) {
165166 // if the error is a property error, use the key position
166167 const isKeyError = positionalErrors . includes ( error . name ) ;
167168 const errorString = this . rewriteError ( error ) ;
@@ -182,8 +183,10 @@ export class JSONValidation {
182183 source : this . schemaTitle ,
183184 } ) ;
184185 }
186+ } else {
187+ pushRoot ( ) ;
185188 }
186189 return acc ;
187- } , [ ] as Diagnostic [ ] ) ;
190+ } , [ ] ) ;
188191 }
189192}
0 commit comments