Operation Contracts
For explicit write semantics, the schema instance exposes three synchronous built-in operation methods:
javascript
userSchema.create(input);
userSchema.replace(input);
userSchema.patch(input);They all return the same { validatedObject, errors } shape, but they differ in how omitted fields are treated:
create(): validates a create payload, enforcesrequired, appliesdefaultTo, and leaves omitted optional fields omitted.replace(): validates a full replacement payload, enforcesrequired, appliesdefaultTo, and preserves omitted fields.patch(): validates only explicitly provided fields and returns only the normalized fields that were touched.
These are built-in named operation contracts, not special cases in the engine. If you need a different contract, define a custom operation and the schema will generate a matching method alias automatically.
Worked operation example
This is easier to understand with one schema and three calls:
javascript
const profileSchema = createSchema({
username: { type: 'string', required: true },
bio: { type: 'string' },
role: { type: 'string', defaultTo: 'member' }
})Calling create():
javascript
profileSchema.create({
username: ' alex '
})Result:
javascript
{
validatedObject: {
username: 'alex',
role: 'member'
},
errors: {}
}Calling replace() with the same payload:
javascript
profileSchema.replace({
username: ' alex '
})Result:
javascript
{
validatedObject: {
username: 'alex',
role: 'member'
},
errors: {}
}Calling patch() with the same payload:
javascript
profileSchema.patch({
username: ' alex '
})Result:
javascript
{
validatedObject: {
username: 'alex'
},
errors: {}
}That difference is the whole point of operation contracts:
create()andreplace()walk the schema as a contract for the whole object.patch()walks only the fields the caller actually touched.- Defaults apply on operations that opt into defaults.
- Missing required fields are only errors on operations that opt into required checks.