1+ import { InvalidMappingError } from "../../errors" ;
12import {
23 SubgraphMapping ,
34 AbiEventMapping ,
@@ -9,43 +10,37 @@ import {
910} from "./actionTypes" ;
1011
1112export const validateSubgraphMapping = ( mapping : ActionMapping ) => {
12- if ( ( mapping as SubgraphMapping ) . endpoint === undefined ) {
13- throw new Error ( "Invalid mapping for graphql action." ) ;
14- }
15- return mapping as SubgraphMapping ;
13+ return validateMapping ( mapping as SubgraphMapping , [ "endpoint" ] ) ;
1614} ;
1715
1816export const validateAbiEventMapping = ( mapping : ActionMapping ) => {
19- if ( ( mapping as AbiEventMapping ) . abi === undefined || ( mapping as AbiEventMapping ) . eventFilter === undefined ) {
20- throw new Error ( "Invalid mapping for abi/event action." ) ;
21- }
22- return mapping as AbiEventMapping ;
17+ return validateMapping ( mapping as AbiEventMapping , [ "abi" , "eventFilter" ] ) ;
2318} ;
2419
2520export const validateAbiCallMapping = ( mapping : ActionMapping ) => {
26- if ( ( mapping as AbiCallMapping ) . abi === undefined || ( mapping as AbiCallMapping ) . functionName === undefined ) {
27- throw new Error ( "Invalid mapping for abi/call action." ) ;
28- }
29- return mapping as AbiCallMapping ;
21+ return validateMapping ( mapping as AbiCallMapping , [ "abi" , "functionName" ] ) ;
3022} ;
3123
3224export const validateJsonMapping = ( mapping : ActionMapping ) => {
33- if ( ( mapping as JsonMapping ) . value === undefined ) {
34- throw new Error ( "Invalid mapping for json action." ) ;
35- }
36- return mapping as JsonMapping ;
25+ return validateMapping ( mapping as JsonMapping , [ "value" ] ) ;
3726} ;
3827
3928export const validateFetchIpfsJsonMapping = ( mapping : ActionMapping ) => {
40- if ( ( mapping as FetchIpfsJsonMapping ) . ipfsUri === undefined ) {
41- throw new Error ( "Invalid mapping for fetch/ipfs/json action." ) ;
42- }
43- return mapping as FetchIpfsJsonMapping ;
29+ return validateMapping ( mapping as FetchIpfsJsonMapping , [ "ipfsUri" ] ) ;
4430} ;
4531
4632export const validateRealityMapping = ( mapping : ActionMapping ) => {
4733 if ( mapping . type !== "reality" || typeof ( mapping as RealityMapping ) . realityQuestionID !== "string" ) {
48- throw new Error ( "Invalid mapping for reality action .") ;
34+ throw new InvalidMappingError ( "Expected field 'realityQuestionID' to be a string .") ;
4935 }
5036 return mapping as RealityMapping ;
5137} ;
38+
39+ const validateMapping = < T extends ActionMapping > ( mapping : T , requiredFields : ( keyof T ) [ ] ) => {
40+ for ( const field of requiredFields ) {
41+ if ( mapping [ field ] === undefined ) {
42+ throw new InvalidMappingError ( `${ field . toString ( ) } is required for ${ mapping . type } ` ) ;
43+ }
44+ }
45+ return mapping ;
46+ } ;
0 commit comments