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.

ts
  import { 
preventChanges
} 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 = {
  /**
   * 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: any, name: string) => 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.
 *
 * @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: (
  fieldNames: MaybeArray<string>,
  options?: PreventChangesOptions,
) => (context: HookContext, next?: _has) => Promise<any>
ArgumentTypeDescription
fieldNamesMaybeArray<string>
optionsPreventChangesOptions
typemethodsmulti
before, aroundpatchyes

Released under the MIT License.