( type: TypeParam, value: Value, config: Config, key: string )
| 24 | type TypeParam = NonNullable<SchemaAttribute['type']>; |
| 25 | |
| 26 | export function validateType( |
| 27 | type: TypeParam, |
| 28 | value: Value, |
| 29 | config: Config, |
| 30 | key: string |
| 31 | ): boolean | ValidationError[] { |
| 32 | if (!type) return true; |
| 33 | |
| 34 | if (Ast.isFunction(value) && config.validation?.validateFunctions) { |
| 35 | const schema = config.functions?.[value.name]; |
| 36 | return !schema?.returns |
| 37 | ? true |
| 38 | : Array.isArray(schema.returns) |
| 39 | ? schema.returns.find((t) => t === type) !== undefined |
| 40 | : schema.returns === type; |
| 41 | } |
| 42 | |
| 43 | if (Ast.isAst(value)) return true; |
| 44 | |
| 45 | if (Array.isArray(type)) |
| 46 | return type.some((t) => validateType(t, value, config, key)); |
| 47 | |
| 48 | if (typeof type === 'string') type = TypeMappings[type]; |
| 49 | |
| 50 | if (typeof type === 'function') { |
| 51 | const instance: any = new type(); |
| 52 | if (instance.validate) { |
| 53 | return instance.validate(value, config, key); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return value != null && value.constructor === type; |
| 58 | } |
| 59 | |
| 60 | function typeToString(type: TypeParam): string { |
| 61 | if (typeof type === 'string') return type; |
no test coverage detected
searching dependent graphs…