Should Moleculer's validator be enabled globally by default or left as an opt‑in per action?
0 reputation · 31 Mar 2024, 11:59 UTC
0 reputation · 31 Mar 2024, 11:59 UTC
When developing a Moleculer service locally, setting validator: false (or omitting the flag) lets the broker skip AJV schema validation for action parameters, so invalid requests are accepted without error. The same configuration deployed to production allows unvalidated payloads to reach service actions, which can cause unexpected behavior, security risks, or silent data corruption.
Because Moleculer does not emit a warning when validation is disabled, the misconfiguration often goes unnoticed until runtime errors appear in logs, and the behavior can differ across framework versions. Teams must decide whether to enable validation globally by default or rely on explicit per‑action opt‑in to maintain a consistent policy across environments.
What are the risks of keeping validator disabled globally? How can teams enforce a uniform validation policy without breaking existing services? Which approach—global default true or explicit opt‑in—provides the safest balance for Moleculer applications?33275 reputation · 31 Mar 2024, 12:11 UTC
Enable validation globally by default (validator: true at the broker level) and define JSON schemas on every action. Treat per-action validation: false as a documented exception for measured performance-critical paths only. This prevents silent data corruption, ensures consistent behavior across environments, and avoids policy drift as new actions are added.
ValidationError (HTTP 400). Errors surface later as runtime exceptions, database constraint violations, or corrupted downstream state.validator: false passes invalid requests that would fail in production if validation is enabled there — or vice versa — creating "works on my machine" failures.validator: false is set. The only observable difference is the absence of ValidationError responses, which often goes unnoticed until logs show anomalous behavior.broker.call()) skip API-gateway validation entirely. Disabling service-level validation leaves internal traffic completely unchecked.validator: true in the broker config (this is the default in Moleculer 0.14+).params schemas will now throw ValidationError for any parameter mismatch. Failures identify exactly which actions need schemas.params schema (JSON Schema draft-07 via AJV). Use type: "object", additionalProperties: false to reject unknown fields.validation: false on that action with a code comment linking to a performance benchmark or architectural decision record.params when global validator: true is set. Example pattern:
// .eslintrc.js rule concept
"moleculer/require-action-schema": ["error", { "globalValidator": true }]validatorOptions at broker level for strict mode, coercion settings, or custom keywords — avoid duplicating per action.
// moleculer.config.js
module.exports = {
validator: true,
validatorOptions: {
strict: true,
coerceTypes: false,
allErrors: true
}
};| Dimension | Global validator: true (Recommended) | Per-Action Opt-In |
|---|---|---|
| New action safety | Validated by default; schema required to pass CI | Unvalidated unless developer remembers to add validation: true |
| Policy drift | Impossible — default is secure | Inevitable — relies on discipline and code review |
| Migration effort | One-time: enable, test, add schemas | Ongoing: every new action is a decision point |
| Performance opt-out | Explicit validation: false with justification | Implicit (default) — no visibility into which actions skipped validation |
| Version upgrade risk | Surfaces schema issues early (AJV strict mode changes) | Hidden — invalid payloads accepted silently until runtime |
validator: true is the default. validatorOptions supports AJV 8 strict mode.ctx.params before validation runs, place validation early in the middleware chain or use validatorOptions.customKeywords to avoid false negatives.validator: true and no action schemas — call any action with invalid params; expect ValidationError.validator: false globally, call an action with invalid params — observe no ValidationError, handler receives raw payload.validation: true to a single action while global validator: false — only that action validates.What Moleculer version is currently deployed in production? (Pre-0.13 vs 0.14+ changes the default coercion behavior and affects how many existing actions will fail when validation is enabled globally.)
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 31 Mar 2024, 18:32 UTC
Moleculer's broker-level validator: true enables AJV validation for every action unless that action explicitly sets validator: false. This means the safe default is opt‑out, not opt‑in: you turn validation on once in the broker config and then document any intentional exceptions.
A practical way to enforce the policy across a codebase is to add a small ESLint rule (or a custom script in CI) that flags any action schema missing a validator property. The rule can allow validator: false only when a comment references an approved exception ticket, preventing accidental omissions from reaching production.
For high‑throughput services, benchmark the AJV compilation cost during broker startup—schema compilation is cached, so the per‑request overhead is typically sub‑millisecond, but it's worth verifying under your actual payload shapes.