MCPcopy
hub / github.com/tinyplex/tinybase / unwrapSchema

Function unwrapSchema

src/schematizers/schematizer-arktype/index.ts:29–78  ·  view source on GitHub ↗
(
  schema: any,
  defaultValue?: any,
  allowNull?: boolean,
)

Source from the content-addressed store, hash-verified

27import {createCustomSchematizer} from '../index.ts';
28
29const unwrapSchema = (
30 schema: any,
31 defaultValue?: any,
32 allowNull?: boolean,
33): [any, any, boolean] => {
34 const schemaData = schema?.json ?? schema;
35
36 if (isArray(schemaData)) {
37 const hasNull = !arrayEvery(
38 schemaData,
39 (item: any) => !isNull(item?.[UNIT]) && !isNull(item),
40 );
41
42 if (
43 size(schemaData) === 2 &&
44 isFalse((schemaData[0] as any)?.[UNIT]) &&
45 isTrue((schemaData[1] as any)?.[UNIT])
46 ) {
47 return [{[TYPE]: BOOLEAN}, defaultValue, allowNull ?? false];
48 }
49
50 if (arrayEvery(schemaData, (item: any) => isString(item?.[UNIT] ?? item))) {
51 return [{[TYPE]: STRING}, defaultValue, allowNull ?? false];
52 }
53
54 if (hasNull) {
55 const nonNullItem = arrayFind(
56 schemaData,
57 (item: any) => !isNull(item?.[UNIT]) && !isNull(item) && item !== '=',
58 );
59 if (nonNullItem) {
60 return unwrapSchema(nonNullItem, defaultValue, true);
61 }
62 }
63 }
64
65 if (!isArray(schemaData) && !isUndefined(schemaData?.[SEQUENCE])) {
66 return [{[TYPE]: ARRAY}, defaultValue, allowNull ?? false];
67 }
68
69 if (!isArray(schemaData) && isString(schemaData?.[UNIT])) {
70 return [{[TYPE]: STRING}, defaultValue, allowNull ?? false];
71 }
72
73 return [
74 {[TYPE]: schemaData?.[DOMAIN] || schemaData},
75 defaultValue,
76 allowNull ?? false,
77 ];
78};
79
80const getProperties = (schema: any) => {
81 const properties: {[key: string]: any} = objNew();

Callers 1

unwrapSchemaWithDefaultsFunction · 0.70

Calls 9

isArrayFunction · 0.90
arrayEveryFunction · 0.90
isNullFunction · 0.90
sizeFunction · 0.90
isFalseFunction · 0.90
isTrueFunction · 0.90
isStringFunction · 0.90
arrayFindFunction · 0.90
isUndefinedFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…