@@ -97,16 +97,17 @@ function isScalar(
9797export function aqua2js (
9898 value : JSONValue ,
9999 schema : NonArrowSimpleType ,
100+ { path } : ValidationContext ,
100101) : JSONValue {
101102 if ( schema . tag === "nil" ) {
102103 return null ;
103104 } else if ( schema . tag === "option" ) {
104105 if ( ! Array . isArray ( value ) ) {
105- throw new SchemaValidationError ( [ ] , schema , "array" , value ) ;
106+ throw new SchemaValidationError ( path , schema , "array" , value ) ;
106107 }
107108
108109 if ( "0" in value ) {
109- return aqua2js ( value [ 0 ] , schema . type ) ;
110+ return aqua2js ( value [ 0 ] , schema . type , { path : [ ... path , "?" ] } ) ;
110111 } else {
111112 return null ;
112113 }
@@ -121,16 +122,16 @@ export function aqua2js(
121122 throw new SchemaValidationError ( [ ] , schema , "array" , value ) ;
122123 }
123124
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 } ]` ] } ) ;
126127 } ) ;
127128 } else if ( schema . tag === "unlabeledProduct" ) {
128129 if ( ! Array . isArray ( value ) ) {
129130 throw new SchemaValidationError ( [ ] , schema , "array" , value ) ;
130131 }
131132
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 } ]` ] } ) ;
134135 } ) ;
135136 } else if ( [ "labeledProduct" , "struct" ] . includes ( schema . tag ) ) {
136137 if ( typeof value !== "object" || value === null || Array . isArray ( value ) ) {
@@ -145,7 +146,7 @@ export function aqua2js(
145146 v = null ;
146147 }
147148
148- const val = aqua2js ( v , type ) ;
149+ const val = aqua2js ( v , type , { path : [ ... path , key ] } ) ;
149150 return [ key , val ] ;
150151 } ) ,
151152 ) ;
@@ -167,7 +168,9 @@ export function js2aqua(
167168 return value ;
168169 } else if ( schema . tag === "option" ) {
169170 // 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 , "?" ] } ) ] ;
171174 } else if ( schema . tag === "topType" ) {
172175 // topType equals to 'any'
173176 return value ;
@@ -221,6 +224,7 @@ export const wrapJsFunction = (
221224 schema :
222225 | ArrowWithoutCallbacks
223226 | ArrowType < LabeledProductType < SimpleTypes > | UnlabeledProductType > ,
227+ funcName : string ,
224228) : ServiceImpl [ string ] => {
225229 return async ( { args, context } ) => {
226230 const schemaArgs =
@@ -236,8 +240,8 @@ export const wrapJsFunction = (
236240 ) ;
237241 }
238242
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 } ]` ] } ) ;
241245 } ) ;
242246
243247 const returnTypeVoid =
@@ -256,6 +260,6 @@ export const wrapJsFunction = (
256260 result = null ;
257261 }
258262
259- return js2aqua ( result , resultSchema , { path : [ ] } ) ;
263+ return js2aqua ( result , resultSchema , { path : [ ` ${ funcName } ReturnValue` ] } ) ;
260264 } ;
261265} ;
0 commit comments