MCPcopy
hub / github.com/markdoc/markdoc / validateType

Function validateType

src/validator.ts:26–58  ·  view source on GitHub ↗
(
  type: TypeParam,
  value: Value,
  config: Config,
  key: string
)

Source from the content-addressed store, hash-verified

24type TypeParam = NonNullable<SchemaAttribute['type']>;
25
26export 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
60function typeToString(type: TypeParam): string {
61 if (typeof type === 'string') return type;

Callers 2

validateFunctionFunction · 0.85
validatorFunction · 0.85

Calls 1

validateMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…