Effect CommunityEC
Effect Community2y ago
20 replies
steida

Exploring ToStruct and FromStruct in TypeScript Schema Validation

Where is ToStruct and FromStruct?

export const table = <Fields extends TableFields>(
  fields: Fields,
  // Because Schema is invariant, we have to do validation like this.
): ValidateFieldsTypes<Fields> extends true
  ? ValidateFieldsNames<Fields> extends true
    ? ValidateFieldsHasId<Fields> extends true
      ? S.Schema<
          Types.Simplify<
            S.ToStruct<Fields> & S.Schema.Type<typeof ReservedColumns>
          >,
          Types.Simplify<
            S.FromStruct<Fields> & S.Schema.From<typeof ReservedColumns>
          >
        >
      : EvoluTypeError<"table() called without id column.">
    : EvoluTypeError<"table() called with a reserved column. Reserved columns are createdAt, updatedAt, isDeleted. Those columns are added by default.">
  : EvoluTypeError<"table() called with unsupported type. Supported types are null, string, number, Uint8Array, JSON Object, and JSON Array. Use SqliteDate for dates and SqliteBoolean for booleans."> =>
  S.struct(fields).pipe(S.extend(ReservedColumns)) as never;
Was this page helpful?