Skip to main content
Version: 0.10.0

exists

Asynchronously checks that the value exists in the backend.

Signature

NguardValidators.Async.exists(config: AsyncValidatorConfig): AsyncValidatorFn

See unique for the full AsyncValidatorConfig (incl. the resolve callback).

Default rule (no resolve)

ResponseResult
2xx (found)valid
404 (not found)invalid { exists: true }
anything elseundecided → valid

Reactive forms

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

productSku = new FormControl('', {
asyncValidators: [NguardValidators.Async.exists({ endpoint: '/api/products/exists' })],
});

Template-driven forms

<input ngModel name="sku" [nguardExists]="{ endpoint: '/api/products/exists' }" />

Custom rule

import { HttpResponse } from '@angular/common/http';

NguardValidators.Async.exists({
endpoint: '/api/products/lookup',
resolve: (res) => (res instanceof HttpResponse ? Array.isArray(res.body) && res.body.length > 0 : null),
});

Error key

{ exists: true }

Notes

  • Must be created in an injection context (the factory calls inject(HttpClient)); the directive handles this.
  • Debounced with request cancellation; empty values pass without a request; transport failures resolve to valid.

See also

  • unique — the opposite check