Grounds
    Preparing search index...

    Function RStruct

    • Creates a struct schema from tagged field definitions.

      Structs map string property names to typed fields with numeric IDs. Encoding uses field IDs (sorted); decoding reconstructs the object.

      Type Parameters

      Parameters

      • fields: T

        Object mapping property names to field() schemas

      Returns TRStruct<T>

      TypeBox schema for struct type

      Basic struct:

      import { RStruct, field, RString, RU8 } from '@grounds/schema';
      import { createCodec, type Static } from '@grounds/schema';

      const UserSchema = RStruct({
      name: field(0, RString()),
      age: field(1, RU8())
      });

      type User = Static<typeof UserSchema>; // { name: string, age: number }

      const codec = createCodec(UserSchema);
      codec.encode({ name: 'Alice', age: 25 });

      Nested structs:

      import { RStruct, RArray, field, RString, RU32 } from '@grounds/schema';

      const AddressSchema = RStruct({
      street: field(0, RString()),
      city: field(1, RString())
      });

      const PersonSchema = RStruct({
      name: field(0, RString()),
      addresses: field(1, RArray(AddressSchema))
      });

      Field IDs determine wire format order, not property names. IDs must be in range 0-127 and unique within the struct. Encoder automatically sorts by field ID; decoder validates sorted order.

      TypeScript infers the correct type via Static, extracting property names and types from the field definitions.

      field for creating individual field schemas