Skip to main content

Function: partial()

Call Signature

partial<T, E>(schemaObj, value): Validation<Partial<T>, FieldError<E>>

Defined in: validation/schema/index.ts:161

Creates a schema validator for partial objects.

Like schema, but only validates fields that are present in the value. Missing fields are not validated.

Type Parameters

T

T extends Record<string, unknown>

E

E

Parameters

schemaObj

ValidationSchema<T, E>

Schema mapping keys to validators

value

Partial<T>

Partial object to validate

Returns

Validation<Partial<T>, FieldError<E>>

Validation with the partial object, or field-level errors

Example

const userSchema: ValidationSchema<User, string> = {
name: (n) => n.length > 0 ? valid(n) : invalid('Name required'),
email: (e) => e.includes('@') ? valid(e) : invalid('Invalid email'),
age: (a) => a >= 18 ? valid(a) : invalid('Must be 18+')
}

// Only validates present fields
partial(userSchema, { name: 'John' })
// => Valid({ name: 'John' })

partial(userSchema, { name: '', email: 'invalid' })
// => Invalid([
// { field: 'name', errors: ['Name required'] },
// { field: 'email', errors: ['Invalid email'] }
// ])

// Real-world: PATCH request validation
app.patch('/api/users/:id', (req, res) => {
const validation = partial(userSchema, req.body)
if (isInvalid(validation)) {
return res.status(400).json({ errors: validation.errors })
}
updateUser(req.params.id, validation.value)
})

See

schema - for validating complete objects

Call Signature

partial<T, E>(schemaObj): (value) => Validation<Partial<T>, FieldError<E>>

Defined in: validation/schema/index.ts:165

Creates a schema validator for partial objects.

Like schema, but only validates fields that are present in the value. Missing fields are not validated.

Type Parameters

T

T extends Record<string, unknown>

E

E

Parameters

schemaObj

ValidationSchema<T, E>

Schema mapping keys to validators

Returns

Validation with the partial object, or field-level errors

(value): Validation<Partial<T>, FieldError<E>>

Parameters

value

Partial<T>

Returns

Validation<Partial<T>, FieldError<E>>

Example

const userSchema: ValidationSchema<User, string> = {
name: (n) => n.length > 0 ? valid(n) : invalid('Name required'),
email: (e) => e.includes('@') ? valid(e) : invalid('Invalid email'),
age: (a) => a >= 18 ? valid(a) : invalid('Must be 18+')
}

// Only validates present fields
partial(userSchema, { name: 'John' })
// => Valid({ name: 'John' })

partial(userSchema, { name: '', email: 'invalid' })
// => Invalid([
// { field: 'name', errors: ['Name required'] },
// { field: 'email', errors: ['Invalid email'] }
// ])

// Real-world: PATCH request validation
app.patch('/api/users/:id', (req, res) => {
const validation = partial(userSchema, req.body)
if (isInvalid(validation)) {
return res.status(400).json({ errors: validation.errors })
}
updateUser(req.params.id, validation.value)
})

See

schema - for validating complete objects