import Ajv from 'ajv' ; import * as apiTypes from './api-types' ; const SCHEMA = { "$schema" : "http://json-schema.org/draft-07/schema#" , "definitions" : { "GetBudgetResponse" : { "type" : "array" , "items" : { "$ref" : "#/definitions/BudgetItem" } } , "BudgetItem" : { "type" : "object" , "properties" : { "year" : { "type" : "number" } , "budget" : { "type" : "object" , "properties" : { "raw" : { "type" : "number" } , "value" : { "type" : "string" } } , "required" : [ "raw" , "value" ] , "additionalProperties" : false } , "used" : { "type" : "object" , "properties" : { "raw" : { "type" : "number" } , "value" : { "type" : "string" } } , "required" : [ "raw" , "value" ] , "additionalProperties" : false } , "balance" : { "type" : "object" , "properties" : { "raw" : { "type" : "number" } , "value" : { "type" : "string" } } , "required" : [ "raw" , "value" ] , "additionalProperties" : false } } , "required" : [ "year" , "budget" , "used" , "balance" ] , "additionalProperties" : false } , "AddBudgetRequest" : { "type" : "object" , "properties" : { "year" : { "type" : "number" } , "budget" : { "type" : "number" } } , "required" : [ "year" , "budget" ] , "additionalProperties" : false } , "AddBudgetResponse" : { "type" : "object" , "properties" : { "ok" : { "type" : "boolean" , "const" : true } } , "required" : [ "ok" ] , "additionalProperties" : false } } } ; const ajv = new Ajv ( { removeAdditional : true } ) . addSchema ( SCHEMA , "SCHEMA" ) ; export function validateGetBudgetResponse ( payload ) { if ( ! isGetBudgetResponse ( payload ) ) { const error = new Error ( 'invalid payload: GetBudgetResponse' ) ; error . name = "ValidationError" ; throw error ; } return payload ; } export function isGetBudgetResponse ( payload ) { /** Schema is defined in  SCHEMA . definitions . GetBudgetResponse * const ajvValidate = ajv . compile ( { "$ref" : "SCHEMA#/definitions/GetBudgetResponse" } ) ; return ajvValidate ( payload ) ; } export function validateBudgetItem ( payload ) { if ( ! isBudgetItem ( payload ) ) { const error = new Error ( 'invalid payload: BudgetItem' ) ; error . name = "ValidationError" ; throw error ; } return payload ; } export function isBudgetItem ( payload ) { /** Schema is defined in  SCHEMA . definitions . BudgetItem * const ajvValidate = ajv . compile ( { "$ref" : "SCHEMA#/definitions/BudgetItem" } ) ; return ajvValidate ( payload ) ; } export function validateAddBudgetRequest ( payload ) { if ( ! isAddBudgetRequest ( payload ) ) { const error = new Error ( 'invalid payload: AddBudgetRequest' ) ; error . name = "ValidationError" ; throw error ; } return payload ; } export function isAddBudgetRequest ( payload ) { /** Schema is defined in  SCHEMA . definitions . AddBudgetRequest * const ajvValidate = ajv . compile ( { "$ref" : "SCHEMA#/definitions/AddBudgetRequest" } ) ; return ajvValidate ( payload ) ; } export function validateAddBudgetResponse ( payload ) { if ( ! isAddBudgetResponse ( payload ) ) { const error = new Error ( 'invalid payload: AddBudgetResponse' ) ; error . name = "ValidationError" ; throw error ; } return payload ; } export function isAddBudgetResponse ( payload ) { /** Schema is defined in  SCHEMA . definitions . AddBudgetResponse * const ajvValidate = ajv . compile ( { "$ref" : "SCHEMA#/definitions/AddBudgetResponse" } ) ; return ajvValidate ( payload ) ; }