@@ -97,16 +97,17 @@ function isScalar(
97
97
export function aqua2js (
98
98
value : JSONValue ,
99
99
schema : NonArrowSimpleType ,
100
+ { path } : ValidationContext ,
100
101
) : JSONValue {
101
102
if ( schema . tag === "nil" ) {
102
103
return null ;
103
104
} else if ( schema . tag === "option" ) {
104
105
if ( ! Array . isArray ( value ) ) {
105
- throw new SchemaValidationError ( [ ] , schema , "array" , value ) ;
106
+ throw new SchemaValidationError ( path , schema , "array" , value ) ;
106
107
}
107
108
108
109
if ( "0" in value ) {
109
- return aqua2js ( value [ 0 ] , schema . type ) ;
110
+ return aqua2js ( value [ 0 ] , schema . type , { path : [ ... path , "?" ] } ) ;
110
111
} else {
111
112
return null ;
112
113
}
@@ -121,16 +122,16 @@ export function aqua2js(
121
122
throw new SchemaValidationError ( [ ] , schema , "array" , value ) ;
122
123
}
123
124
124
- return value . map ( ( y ) => {
125
- return aqua2js ( y , schema . type ) ;
125
+ return value . map ( ( y , i ) => {
126
+ return aqua2js ( y , schema . type , { path : [ ... path , `[ ${ i } ]` ] } ) ;
126
127
} ) ;
127
128
} else if ( schema . tag === "unlabeledProduct" ) {
128
129
if ( ! Array . isArray ( value ) ) {
129
130
throw new SchemaValidationError ( [ ] , schema , "array" , value ) ;
130
131
}
131
132
132
- return zip ( value , schema . items ) . map ( ( [ v , s ] ) => {
133
- return aqua2js ( v , s ) ;
133
+ return zip ( value , schema . items ) . map ( ( [ v , s ] , i ) => {
134
+ return aqua2js ( v , s , { path : [ ... path , `[ ${ i } ]` ] } ) ;
134
135
} ) ;
135
136
} else if ( [ "labeledProduct" , "struct" ] . includes ( schema . tag ) ) {
136
137
if ( typeof value !== "object" || value === null || Array . isArray ( value ) ) {
@@ -145,7 +146,7 @@ export function aqua2js(
145
146
v = null ;
146
147
}
147
148
148
- const val = aqua2js ( v , type ) ;
149
+ const val = aqua2js ( v , type , { path : [ ... path , key ] } ) ;
149
150
return [ key , val ] ;
150
151
} ) ,
151
152
) ;
@@ -167,7 +168,9 @@ export function js2aqua(
167
168
return value ;
168
169
} else if ( schema . tag === "option" ) {
169
170
// option means 'type | null'
170
- return value === null ? [ ] : [ js2aqua ( value , schema . type , { path } ) ] ;
171
+ return value === null
172
+ ? [ ]
173
+ : [ js2aqua ( value , schema . type , { path : [ ...path , "?" ] } ) ] ;
171
174
} else if ( schema . tag === "topType" ) {
172
175
// topType equals to 'any'
173
176
return value ;
@@ -221,6 +224,7 @@ export const wrapJsFunction = (
221
224
schema :
222
225
| ArrowWithoutCallbacks
223
226
| ArrowType < LabeledProductType < SimpleTypes > | UnlabeledProductType > ,
227
+ funcName : string ,
224
228
) : ServiceImpl [ string ] => {
225
229
return async ( { args, context } ) => {
226
230
const schemaArgs =
@@ -236,8 +240,8 @@ export const wrapJsFunction = (
236
240
) ;
237
241
}
238
242
239
- const jsArgs = zip ( args , schemaArgs ) . map ( ( [ arg , schemaArg ] ) => {
240
- return aqua2js ( arg , schemaArg ) ;
243
+ const jsArgs = zip ( args , schemaArgs ) . map ( ( [ arg , schemaArg ] , i ) => {
244
+ return aqua2js ( arg , schemaArg , { path : [ ` ${ funcName } Args` , `[ ${ i } ]` ] } ) ;
241
245
} ) ;
242
246
243
247
const returnTypeVoid =
@@ -256,6 +260,6 @@ export const wrapJsFunction = (
256
260
result = null ;
257
261
}
258
262
259
- return js2aqua ( result , resultSchema , { path : [ ] } ) ;
263
+ return js2aqua ( result , resultSchema , { path : [ ` ${ funcName } ReturnValue` ] } ) ;
260
264
} ;
261
265
} ;
0 commit comments