Skip to content

Type Alias: EncoderOptions ​

ts
type EncoderOptions = {
  allowEncoderFunctions?: boolean;
  allowFunctions?: boolean;
  allowSchemaFunctions?: boolean;
  budget?: DecodeBudget;
  limits?: DecodeLimits;
  onDisallowedFunction?: "silent" | "throw";
  withDatabase?: boolean;
};

Options for encoding and decoding validation schemas.

These options control what gets serialized when encoding schemas and how they're reconstructed during decoding. The defaults prioritize security and are designed for the most common use case: sharing validation rules between frontend and backend environments.

Properties ​

PropertyTypeDescription
allowEncoderFunctions?booleanAllow the bundled @nhtio/encoder to rehydrate fns:d records. Falls back to allowFunctions. Rehydrated functions are built from payload text with the Function constructor. Only enable this for schemas from a trusted, authenticated source (your own backend), never user-controlled input.
allowFunctions?booleanAllow both encoder and schema functions to be rehydrated. Defaults to false. Rehydrated functions are built from payload text with the Function constructor. Only enable this for schemas from a trusted, authenticated source (your own backend), never user-controlled input.
allowSchemaFunctions?booleanAllow validation to rehydrate its own 'function' entries (.custom() rules). Falls back to allowFunctions; when neither is set, DB functions follow withDatabase. Rehydrated functions are built from payload text with the Function constructor. Only enable this for schemas from a trusted, authenticated source (your own backend), never user-controlled input.
budget?DecodeBudgetShare an encoder decode budget across multiple validation decodes. Used for the schema payload and all its embedded flags instead of creating a new budget. Cannot be combined with limits; passing both throws TypeError.
limits?DecodeLimitsPer-call encoder decode limits. Defaults to 16 MiB total inflated bytes, nesting depth 32, and 1,048,576 array/typed-array/buffer/collection entries. The inflate budget is shared across the schema payload and all its embedded flags; depth and entry caps apply per encoder decode call. Raise only for trusted payloads; Infinity disables a limit. Invalid values are rejected by the encoder. Cannot be combined with budget. Limit violations always propagate unchanged as E_UNDECODABLE_VALUE.
onDisallowedFunction?"silent" | "throw"What happens to a disallowed function: 'throw' (default) makes decode() throw; 'silent' replaces it with a no-op and will change validation results, so use it only when intentional. 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 input.
withDatabase?booleanInclude database connections and database validation rules in the encoded schema. When false (default): - Database connections (.knex(), .db()) are stripped from the encoded schema - Database validation rules (.uniqueInDb(), .existsInDb()) are converted to no-ops - Filter functions in database rules are replaced with empty functions - This prevents credential leakage and reduces payload size When true: - Database connection configurations are serialized (if they're plain objects) - Database validation rules and their filter functions are preserved and rehydrated when decoding - Security warning: This is a trusted-backend opt-in; allowSchemaFunctions: false explicitly disables DB function rehydration - Encoded schemas will require re-attaching database connections after decoding Why this defaults to false: 1. Security: Prevents accidental exposure of database credentials in client-side code 2. Use case alignment: Frontend validation typically doesn't need database rules 3. Credential safety: Database connections often contain sensitive connection strings 4. Function security: Custom filter functions may contain business logic or credentials Default false Examples // Backend const schema = joi.object({ email: joi.string().email().uniqueInDb('users', 'email') }).knex(db) const encoded = encode(schema) // withDatabase defaults to false // Database rules are stripped, safe to send to frontend // Frontend receives and decodes const frontendSchema = decode(encoded) // Only gets email format validation, no database checks // Service A const schema = joi.object({ username: joi.string().uniqueInDb('users', 'username', { filter: async (query) => query.where('tenant_id', tenantId) }) }).knex({ client: 'pg', connection: { host: 'localhost', ... } }) const encoded = encode(schema, { withDatabase: true }) // Sends to trusted Service B // Service B const decoded = decode(encoded, { withDatabase: true }) const rehydrated = decoded.knex(serviceB_db_connection) // DB rules are rehydrated by withDatabase. Any .custom() rule in this schema // additionally requires allowSchemaFunctions: true. // Now has full database validation capabilities