Skip to content

encodeCborSchema

encodeCborSchema(schema): string

(Internal Function) - Serializes a given schema object using CBOR encoding for consistent hashing, comparison, or storage. This ensures a standardized representation by ignoring the $id field (if present) and sorting the schema properties deterministically.

Functionality:

  • Removes $id: Strips the $id field from the schema to ensure consistent serialization.
  • Sorts properties: Uses a deterministic sorting algorithm to guarantee the same encoding for logically identical schemas, crucial for hashing.
  • CBOR Encoding: Encodes the sorted schema in CBOR (Concise Binary Object Representation), a compact binary format suitable for storage and transmission.
  • Base64 Conversion: Converts the encoded schema to a Base64 string, facilitating storage, transmission, and hashing.

Parameters:

Parameters

schema: any

The schema object to be serialized. It can include or exclude the $id field, as this field is ignored during serialization for consistency.

Returns:

Returns

string

A Base64 string representing the serialized CBOR encoding of the schema (without the $id field). This string can be used for hashing, comparison, or storage.

Example Usage:

const schema = {
title: 'Example Schema',
properties: { name: { type: 'string' }, age: { type: 'number' } },
$id: 'schema-id'
};
const encodedSchema = encodeCborSchema(schema);
console.log('Encoded CBOR Schema:', encodedSchema);

Internal Usage:

This function is primarily intended for internal use, where schema objects need to be hashed or compared without being affected by non-functional fields like $id.

Source

schema/src/Schema.ts:109