JavaScript value matching the schema type
Relish schema defining the value structure
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:
Both take/return the same format (bytes), making the API intuitive and composable with streaming operations.
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.