Variable: decode()
const decode: (base64: string, options?: EncoderOptions) => Schema;Decodes a base64-encoded schema string back into a usable validation schema.
This function reverses the encoding process, decompressing and parsing the encoded schema while performing version compatibility checks. It ensures that schemas encoded with newer versions of the library are rejected to prevent runtime errors from incompatible schema formats.
The function handles:
- Base64 decoding and zlib decompression
- Version compatibility validation using semantic versioning
- Backward compatibility with legacy encoding formats
- Input validation and error handling
- Schema reconstruction using Joi's build method
Parameters
| Parameter | Type | Description |
|---|---|---|
base64 | string | The base64-encoded schema string |
options? | EncoderOptions | Decoding options: - allowFunctions enables both ordinary schema and encoder function rehydration (default false). - withDatabase: true additionally rehydrates database-rule functions unless explicitly disabled. - allowSchemaFunctions enables validation schema functions; overrides allowFunctions. - allowEncoderFunctions enables encoder fns:d functions; overrides allowFunctions. - onDisallowedFunction selects 'throw' (default) or 'silent' (substitutes a no-op). - withDatabase retains database connections and rules (default false). - limits resource limits forwarded to @nhtio/encoder: maxInflatedBytes (default 16 MiB), maxDepth (default 32), and maxArrayLength (default 1,048,576). Limit violations throw E_UNDECODABLE_VALUE. - Schema keys named __proto__, constructor, or prototype are unsupported and rejected on decode. |
Returns
A reconstructed validation schema ready for validation
Throws
When the encoded string is not a valid schema
Throws
When the schema version is invalid or incompatible
Examples
import { decode } from "./utils";
const encodedSchema = "eJyrVkosLcmIz8nPS1WyUvJIzcnJT...";
const schema = decode(encodedSchema);
// Use the decoded schema for validation
const result = schema.validate({ name: "John", age: 30 });try {
const schema = decode(untrustedEncodedString);
// Use schema safely
} catch (error) {
if (error instanceof TypeError) {
console.error("Invalid or incompatible schema:", error.message);
}
}// Schema encoded with v2.1.0, current version is v2.0.0
const futureSchema = "..."; // Contains version 2.1.0
decode(futureSchema); // Throws: Schema version 2.1.0 is not compatible
// Schema encoded with v1.9.0, current version is v2.0.0
const oldSchema = "..."; // Contains version 1.9.0
const schema = decode(oldSchema); // Works - backward compatibleconst decoded = decode(encoded, { allowSchemaFunctions: true });const encoded = encode(schema, { withDatabase: true, allowFunctions: true });
const decoded = decode(encoded, { withDatabase: true, allowFunctions: true });Security
Function rehydration is disabled by default (allowFunctions, allowSchemaFunctions, and allowEncoderFunctions all default to false); disallowed functions throw by default. Rehydrated functions are built from payload text with the Function constructor. Only opt in for schemas from a trusted, authenticated source (your own backend), never user-controlled or untrusted third-party input. 'silent' replaces disallowed functions with no-ops and will change validation results, so use it only when that behavior is intentional. Use cryptographic signatures or checksums to verify payload integrity.
See
encode For encoding schemas into transportable strings