Skip to main content
Version: 0.9.0

inArray

Validate that the value is a member of a sibling field's array. Fails when the sibling is missing or is not an array.

Signature

NguardValidators.Array.inArray(fieldKey: string, isStrict?: boolean): ValidatorFn
ParameterTypeDefaultDescription
fieldKeystringThe key of the sibling field holding the array
isStrictbooleantrueIf true, membership uses strict equality (===); loose (==) otherwise

Reactive forms

import { FormControl, FormGroup } from '@angular/forms';
import { NguardValidators } from 'ng-nguard';

new FormGroup({
allowedColors: new FormControl(['red', 'green']),
color: new FormControl('red', [NguardValidators.Array.inArray('allowedColors')]),
});

Template-driven forms

<!-- Shorthand: strict membership in the 'allowedColors' sibling -->
<input ngModel name="color" [nguardInArray]="'allowedColors'" />

<!-- Object form: loose membership -->
<input ngModel name="id" [nguardInArray]="{ fieldKey: 'allowedIds', isStrict: false }" />

Error key

{ inArray: true }

Notes

  • Reads control.parent?.get(fieldKey)?.value. Fails when that sibling is missing or not an array.
  • Strict by default; pass isStrict: false (object form) to allow type coercion ('1' matches 1).

See also

  • contains — array contains required members