MCPcopy Create free account
hub / github.com/blocknative/sdk / validateType

Function validateType

src/validation.ts:4–33  ·  view source on GitHub ↗
(options: {
  name: string
  value: any
  type: string
  optional?: boolean
  customValidation?: (val: any) => boolean
})

Source from the content-addressed store, hash-verified

2import { DEPRECATED_NETWORK_IDS, networks } from './defaults'
3
4export function validateType(options: {
5 name: string
6 value: any
7 type: string
8 optional?: boolean
9 customValidation?: (val: any) => boolean
10}): never | void {
11 const { name, value, type, optional, customValidation } = options
12
13 if (!optional && typeof value === 'undefined') {
14 throw new Error(`"${name}" is required`)
15 }
16
17 if (
18 typeof value !== 'undefined' &&
19 (type === 'array' ? Array.isArray(type) : typeof value !== type)
20 ) {
21 throw new Error(
22 `"${name}" must be of type: ${type}, received type: ${typeof value} from value: ${value}`
23 )
24 }
25
26 if (
27 typeof value !== 'undefined' &&
28 customValidation &&
29 !customValidation(value)
30 ) {
31 throw new Error(`"${value}" is not a valid "${name}"`)
32 }
33}
34
35export function validateOptions(options: any): never | void {
36 validateType({ name: 'sdk options', value: options, type: 'object' })

Callers 1

validateOptionsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected