import Ajv = require ( 'ajv' ) ; import { StateChannelsRequest , StateChannelsResponse , StateChannelsNotification , StateChannelsErrorResponse } from './types' ; const ajv = new Ajv ( { jsonPointers : true , verbose : true } ) ; const apiSchema = require ( './generated-schema.json' ) ; ajv . addSchema ( apiSchema , 'api.json' ) ; export const validateRequest = ajv . compile ( { $ref : 'api.json#/definitions/StateChannelsRequest' } ) ; export const validateResponse = ajv . compile ( { $ref : 'api.json#/definitions/StateChannelsResponse' } ) ; export const validateErrorResponse = ajv . compile ( { $ref : 'api.json#/definitions/StateChannelsErrorResponse' } ) ; export const validateNotification = ajv . compile ( { $ref : 'api.json#/definitions/StateChannelsNotification' } ) ; function prettyPrintError ( e ) { switch ( e . keyword ) { case 'additionalProperties' : { const unexpected = ( e . params as any ) . additionalProperty ; return `Unexpected property '${ unexpected }' found at root${ e . dataPath } ` ; } case 'required' : { const missing = ( e . params as any ) . missingProperty ; return `Missing required property '${ missing }' at root${ e . dataPath }` ; } case 'type' : case 'pattern' : { return `Property at root${ e . dataPath } ${ e . message }` ; } } return JSON . stringify ( e ) ; } param jsonBlob - A javascript object that might be a valid  StateChannelsRequest returns export function parseRequest ( jsonBlob ) { const valid = validateRequest ( jsonBlob ) ; if ( ! valid ) { throw new Error ( `Validation Error: ${ validateRequest . errors ?. map ( e => prettyPrintError ( e ) ) . join ( `;\n` ) }` ) ; } return jsonBlob as StateChannelsRequest ; } param jsonBlob - A javascript object that might be a valid  StateChannelsResponse returns export function parseResponse ( jsonBlob ) { const valid = validateResponse ( jsonBlob ) ; if ( ! valid ) { throw new Error ( `
      Validation Error:
        input: ${ JSON . stringify ( jsonBlob ) };\n
        ${ validateResponse . errors ?. map ( e => prettyPrintError ( e ) ) . join ( `;\n` ) }
      ` ) ; } return jsonBlob as StateChannelsResponse ; } param jsonBlob - A javascript object that might be a valid  StateChannelsNotification returns export function parseNotification ( jsonBlob ) { const valid = validateNotification ( jsonBlob ) ; if ( ! valid ) { throw new Error ( `
      Validation Error:
        input: ${ JSON . stringify ( jsonBlob ) };\n
        ${ validateNotification . errors ?. map ( e => prettyPrintError ( e ) ) . join ( `;\n` ) }
      ` ) ; } return jsonBlob as StateChannelsNotification ; } param jsonBlob - A javascript object that might be a valid  StateChannelsErrorResponse returns export function parseErrorResponse ( jsonBlob ) { const valid = validateErrorResponse ( jsonBlob ) ; if ( ! valid ) { throw new Error ( `
      Validation Error:
        input: ${ JSON . stringify ( jsonBlob ) };\n
        ${ validateNotification . errors ?. map ( e => prettyPrintError ( e ) ) . join ( `;\n` ) }
      ` ) ; } return jsonBlob as StateChannelsErrorResponse ; }