( unwrapSchema, getProperties, )
| 19 | } from '../common/strings.ts'; |
| 20 | |
| 21 | export const createCustomSchematizer: typeof createCustomSchematizerDecl = ( |
| 22 | unwrapSchema, |
| 23 | getProperties, |
| 24 | ) => { |
| 25 | const toCellOrValueSchema = ( |
| 26 | schema: any, |
| 27 | ): CellSchema | ValueSchema | undefined => { |
| 28 | const [unwrapped, defaultValue, allowNull] = unwrapSchema(schema); |
| 29 | const type = unwrapped?.type; |
| 30 | |
| 31 | if ( |
| 32 | type !== STRING && |
| 33 | type !== NUMBER && |
| 34 | type !== BOOLEAN && |
| 35 | type !== OBJECT && |
| 36 | type !== ARRAY |
| 37 | ) { |
| 38 | return undefined; |
| 39 | } |
| 40 | |
| 41 | const cellOrValueSchema: CellSchema = {[TYPE]: type} as CellSchema; |
| 42 | ifNotUndefined(defaultValue, (defaultValue) => { |
| 43 | (cellOrValueSchema as any)[DEFAULT] = defaultValue; |
| 44 | }); |
| 45 | if (allowNull) { |
| 46 | (cellOrValueSchema as any)[ALLOW_NULL] = true; |
| 47 | } |
| 48 | return cellOrValueSchema; |
| 49 | }; |
| 50 | |
| 51 | const toTablesSchema = (schemas: {[tableId: string]: any}): TablesSchema => { |
| 52 | const tablesSchema: TablesSchema = objNew(); |
| 53 | objForEach(schemas, (schema, tableId) => { |
| 54 | const tableSchema: {[cellId: string]: CellSchema} = objNew(); |
| 55 | ifNotUndefined(getProperties(schema), (properties) => |
| 56 | objForEach(properties, (cellSchema, cellId) => |
| 57 | ifNotUndefined(toCellOrValueSchema(cellSchema), (cellSchema) => { |
| 58 | tableSchema[cellId] = cellSchema; |
| 59 | }), |
| 60 | ), |
| 61 | ); |
| 62 | if (!objIsEmpty(tableSchema)) { |
| 63 | tablesSchema[tableId] = tableSchema; |
| 64 | } |
| 65 | }); |
| 66 | return tablesSchema; |
| 67 | }; |
| 68 | |
| 69 | const toValuesSchema = (schemas: {[valueId: string]: any}): ValuesSchema => { |
| 70 | const valuesSchema: ValuesSchema = objNew(); |
| 71 | objForEach(schemas, (schema, valueId) => |
| 72 | ifNotUndefined(toCellOrValueSchema(schema), (valueSchema) => { |
| 73 | valuesSchema[valueId] = valueSchema as ValueSchema; |
| 74 | }), |
| 75 | ); |
| 76 | return valuesSchema; |
| 77 | }; |
| 78 |
no outgoing calls
no test coverage detected
searching dependent graphs…