Automattic / Automattic/mongoose

DISCUSSION: Optimizing validation

Open
#2,332 1 comment 0 reactions 0 assignees View on GitHub
discussion
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

My schema has:
- field validation for basic type, required, range, domain, etc. validation;
- pre-validation middleware that accesses the database to verify complex business rules.

I would like to avoid hitting the database if field validation has failed.

It seems pre-validation middleware is executed before field validation and post-validation does not trigger validation errors, which makes it no different from pre-save.

How do you recommend I achieve this?

Is it an opportunity to revise post-validate or implement a new hook? I recommend that post-validate:
- is only executed if pre-validate and field validation execute successfully;
- accrues validation errors via document.invalidate.

Contributor guide

Open the contributing guide

Research direction

No files, tests, or entry points are named. Start by tracing the validation hook lifecycle and compare pre-validation, field validation, post-validation, and pre-save behavior. Done would require an agreed hook design that avoids database work after field-validation failure and defines how later validation errors are accumulated.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.