Skip to content

preventChanges

hooks
Source Code | Documentation

Prevents patch calls from modifying certain fields. By default, the protected fields are silently removed from context.data. When error is set, a BadRequest is thrown if any protected field is present.

Supports dot.notation for nested fields.

ts
  import {  } from 'feathers-utils/hooks';

Example

ts
import { preventChanges } from 'feathers-utils/hooks'

app.service('users').hooks({
  before: { patch: [preventChanges(['email', 'role'], { error: true })] }
})

Type declaration

Show Type Declarations
ts
export type PreventChangesOptions<D, Keys extends KeyOfOrDotNotation<D>> = {
  /**
   * Customize the error that is thrown if the service tries to patch a field that is not allowed.
   *
   * If not provided, throws a `BadRequest` error with a message indicating the field that is not allowed.
   */
  error?: boolean | ((item: D, name: Keys) => FeathersError)
}
/**
 * Prevents `patch` calls from modifying certain fields. By default, the protected
 * fields are silently removed from `context.data`. When `error` is set, a `BadRequest`
 * is thrown if any protected field is present.
 *
 * Supports dot.notation for nested fields.
 *
 * @example
 * ```ts
 *
 *
 * app.service('users').hooks({
 *   before: { patch: [preventChanges(['email', 'role'], { error: true })] }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/hooks/prevent-changes.html
 */
export declare const preventChanges: <
  H extends HookContext = HookContext,
  D extends DataSingleHookContext<H> = DataSingleHookContext<H>,
  Keys extends KeyOfOrDotNotation<D> = KeyOfOrDotNotation<D>,
>(
  fieldNames: MaybeArray<Keys>,
  options?: PreventChangesOptions<D, Keys>,
) => (context: H, next?: _has) => Promise<any>
ArgumentTypeDescription
fieldNamesMaybeArray<Keys>
optionsPreventChangesOptions<D, Keys>
typemethodsmulti
before, aroundpatchyes

Released under the MIT License.