@@ -41,19 +41,23 @@ export interface Declaration {
4141 * Normalize and check a declaration.
4242 *
4343 * `schema` is required; `uiSchema` is optional and `undefined` means absent.
44+ *
45+ * `label` names the component the author actually wrote, so a diagnostic from a
46+ * form served for `<Elicit>` does not claim to come from `<WebForm>`. It mirrors
47+ * `compileParseSchema(componentName, …)` in core.
4448 */
45- export function parseDeclaration ( schema : unknown , uiSchema ?: unknown ) : Declaration {
46- const normalizedSchema = readObject ( "schema" , schema ) ;
49+ export function parseDeclaration ( label : string , schema : unknown , uiSchema ?: unknown ) : Declaration {
50+ const normalizedSchema = readObject ( label , "schema" , schema ) ;
4751
48- rejectAsyncSchema ( normalizedSchema ) ;
49- rejectUnsupportedNames ( normalizedSchema ) ;
50- rejectExternalReferences ( normalizedSchema ) ;
51- rejectInvalidDraft07 ( normalizedSchema ) ;
52+ rejectAsyncSchema ( label , normalizedSchema ) ;
53+ rejectUnsupportedNames ( label , normalizedSchema ) ;
54+ rejectExternalReferences ( label , normalizedSchema ) ;
55+ rejectInvalidDraft07 ( label , normalizedSchema ) ;
5256
5357 if ( uiSchema === undefined ) {
5458 return { schema : normalizedSchema } ;
5559 }
56- return { schema : normalizedSchema , uiSchema : readObject ( "uiSchema" , uiSchema ) } ;
60+ return { schema : normalizedSchema , uiSchema : readObject ( label , "uiSchema" , uiSchema ) } ;
5761}
5862
5963/**
@@ -64,26 +68,26 @@ export function parseDeclaration(schema: unknown, uiSchema?: unknown): Declarati
6468 * asserted one, and it costs one walk of a value that is about to be walked
6569 * again anyway.
6670 */
67- function readObject ( name : string , value : unknown ) : JsonObject {
71+ function readObject ( label : string , name : string , value : unknown ) : JsonObject {
6872 if ( typeof value === "string" ) {
6973 let decoded : unknown ;
7074 try {
7175 decoded = JSON . parse ( value ) ;
7276 } catch ( error ) {
73- throw new DeclarationError ( `<WebForm > ${ name } text is not JSON: ${ messageOf ( error ) } ` ) ;
77+ throw new DeclarationError ( `<${ label } > ${ name } text is not JSON: ${ messageOf ( error ) } ` ) ;
7478 }
75- return asObject ( name , decoded ) ;
79+ return asObject ( label , name , decoded ) ;
7680 }
77- return asObject ( name , value ) ;
81+ return asObject ( label , name , value ) ;
7882}
7983
80- function asObject ( name : string , value : unknown ) : JsonObject {
84+ function asObject ( label : string , name : string , value : unknown ) : JsonObject {
8185 try {
8286 return parseJsonObject ( value ) ;
8387 } catch ( error ) {
8488 if ( error instanceof JsonParseError ) {
8589 throw new DeclarationError (
86- `<WebForm > ${ name } must be a JSON object or JSON text describing one: ${ error . message } ` ,
90+ `<${ label } > ${ name } must be a JSON object or JSON text describing one: ${ error . message } ` ,
8791 ) ;
8892 }
8993 throw error ;
@@ -98,10 +102,10 @@ function asObject(name: string, value: unknown): JsonObject {
98102 * sides, so such a validator would report every submission as valid — a promise
99103 * is truthy. Refusing the declaration is the only place this is cheap to see.
100104 */
101- function rejectAsyncSchema ( schema : JsonObject ) : void {
105+ function rejectAsyncSchema ( label : string , schema : JsonObject ) : void {
102106 if ( schema [ "$async" ] === true ) {
103107 throw new DeclarationError (
104- "<WebForm > schema must not be asynchronous ($async: true): submission validation is synchronous." ,
108+ `< ${ label } > schema must not be asynchronous ($async: true): submission validation is synchronous.` ,
105109 ) ;
106110 }
107111}
@@ -122,15 +126,15 @@ function rejectAsyncSchema(schema: JsonObject): void {
122126 * not have. The same string as *data* — a `const`, an `enum` member, a title, a
123127 * default — is untouched, because nothing reads it as a key.
124128 */
125- function rejectUnsupportedNames ( schema : JsonObject ) : void {
129+ function rejectUnsupportedNames ( label : string , schema : JsonObject ) : void {
126130 walkSchema ( schema , {
127131 subschema ( ) { } ,
128132 declaredName ( name : string , kind : NameKind , path : string ) {
129133 if ( name !== "__proto__" ) {
130134 return ;
131135 }
132136 throw new DeclarationError (
133- `<WebForm > schema declares "__proto__" as a ${ kind } at ${ path } , which is not ` +
137+ `<${ label } > schema declares "__proto__" as a ${ kind } at ${ path } , which is not ` +
134138 "supported: the underlying validator loses that name, so the rule would " +
135139 "silently not apply. Rename it, or carry the value under a different key." ,
136140 ) ;
@@ -151,15 +155,15 @@ function rejectUnsupportedNames(schema: JsonObject): void {
151155 * a `const` or an `enum` member is a JSON value the author wants matched, not a
152156 * reference, and Ajv never resolves it — so neither does this.
153157 */
154- function rejectExternalReferences ( schema : JsonObject ) : void {
158+ function rejectExternalReferences ( label : string , schema : JsonObject ) : void {
155159 walkSchema ( schema , {
156160 subschema ( subschema : JsonObject , path : string ) {
157161 const reference = subschema [ "$ref" ] ;
158162 if ( typeof reference !== "string" || reference . startsWith ( "#" ) ) {
159163 return ;
160164 }
161165 throw new DeclarationError (
162- `<WebForm > schema references "${ reference } " at ${ path } , which is outside the ` +
166+ `<${ label } > schema references "${ reference } " at ${ path } , which is outside the ` +
163167 "supplied schema. Only references contained within it resolve; external file " +
164168 "and HTTP(S) references are deferred to #192." ,
165169 ) ;
@@ -176,13 +180,13 @@ function rejectExternalReferences(schema: JsonObject): void {
176180 * pointer that does not resolve is not visible here — it surfaces when the
177181 * compiler runs, and is reported there.
178182 */
179- function rejectInvalidDraft07 ( schema : JsonObject ) : void {
183+ function rejectInvalidDraft07 ( label : string , schema : JsonObject ) : void {
180184 const ajv = createServerAjv ( ) ;
181185 if ( ajv . validateSchema ( schema ) ) {
182186 return ;
183187 }
184188 throw new DeclarationError (
185- `<WebForm > schema is not a valid draft-07 JSON Schema: ${ ajv . errorsText ( ajv . errors ) } ` ,
189+ `<${ label } > schema is not a valid draft-07 JSON Schema: ${ ajv . errorsText ( ajv . errors ) } ` ,
186190 ) ;
187191}
188192
0 commit comments