Grounds
    Preparing search index...

    Function toRelish

    • Converts a JavaScript value to Relish binary bytes using a schema.

      Validates the value against the schema, converts to Relish wire format, and encodes to bytes in a single operation.

      Parameters

      • value: unknown

        JavaScript value matching the schema type

      • schema: TRelishSchema

        Relish schema defining the value structure

      Returns Result<Uint8Array<ArrayBufferLike>, EncodeError>

      Result containing encoded bytes, or EncodeError on failure

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

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

      toRelish({ name: 'Alice', age: 25 }, schema).match(
      (bytes) => {
      // bytes: Uint8Array ready for transmission or storage
      console.log('Encoded:', bytes.length, 'bytes');
      },
      (error) => console.error('Encoding failed:', error.message)
      );

      This function provides true API symmetry with fromRelish per ADR 0001:

      • toRelish: JS value → bytes
      • fromRelish: bytes → JS value

      Both take/return the same format (bytes), making the API intuitive and composable with streaming operations.

      • fromRelish for the inverse operation (bytes → value)
      • createCodec for a wrapped API with type inference