Skip to content

Commit

Permalink
feat: add standalone validator
Browse files Browse the repository at this point in the history
  • Loading branch information
illfixit committed Sep 15, 2024
1 parent 291a488 commit dbf324a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
58 changes: 58 additions & 0 deletions src/app/core/validators/data-offer-form-validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {Injectable} from '@angular/core';
import {AbstractControl, ValidationErrors, ValidatorFn} from '@angular/forms';
import {Observable, of} from 'rxjs';
import {catchError, map} from 'rxjs/operators';
import {IdAvailabilityResponse} from '@sovity.de/edc-client';
import {EdcApiService} from '../services/api/edc-api.service';

@Injectable({
providedIn: 'root',
})
export class DataOfferFormValidators {
constructor(private edcApiService: EdcApiService) {}

policyIdExistsValidator: ValidatorFn = (
control: AbstractControl,
): Observable<ValidationErrors | null> => {
const value = control.value;
if (value) {
return this.edcApiService.isPolicyIdAvailable(value).pipe(
catchError(() =>
of<IdAvailabilityResponse>({id: value, available: false}),
),
map((it) => (it.available ? null : {idAlreadyExists: true})),
);
}
return of(null);
};

contractDefinitionIdExistsValidator: ValidatorFn = (
control: AbstractControl,
): Observable<ValidationErrors | null> => {
const value = control.value;
if (value) {
return this.edcApiService.isContractDefinitionIdAvailable(value).pipe(
catchError(() =>
of<IdAvailabilityResponse>({id: value, available: false}),
),
map((it) => (it.available ? null : {idAlreadyExists: true})),
);
}
return of(null);
};

assetIdExistsValidator: ValidatorFn = (
control: AbstractControl,
): Observable<ValidationErrors | null> => {
const value = control.value;
if (value) {
return this.edcApiService.isAssetIdAvailable(value).pipe(
catchError(() =>
of<IdAvailabilityResponse>({id: value, available: false}),
),
map((it) => (it.available ? null : {idAlreadyExists: true})),
);
}
return of(null);
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Injectable} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {PolicyDefinitionDto} from '@sovity.de/edc-client';
import {EditAssetFormValidators} from 'src/app/component-library/edit-asset-form/edit-asset-form/form/edit-asset-form-validators';
import {DataOfferFormValidators} from 'src/app/core/validators/data-offer-form-validators';
import {UiAssetMapped} from '../../../../core/services/models/ui-asset-mapped';
import {noWhitespacesOrColonsValidator} from '../../../../core/validators/no-whitespaces-or-colons-validator';
import {
Expand All @@ -25,15 +25,15 @@ export class ContractDefinitionEditorDialogForm {

constructor(
private formBuilder: FormBuilder,
private validators: EditAssetFormValidators,
private validators: DataOfferFormValidators,
) {}

buildFormGroup(): FormGroup<ContractDefinitionEditorDialogFormModel> {
return this.formBuilder.nonNullable.group({
id: [
'',
[Validators.required, noWhitespacesOrColonsValidator],
[this.validators.isValidDataOfferId()],
[this.validators.contractDefinitionIdExistsValidator],
],
accessPolicy: [null as PolicyDefinitionDto | null, Validators.required],
contractPolicy: [null as PolicyDefinitionDto | null, Validators.required],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ <h1 mat-dialog-title>Publish New Data Offer</h1>
<mat-error *ngIf="ctrl.invalid && ctrl.errors?.pattern">{{
validationMessages.invalidWhitespacesOrColonsMessage
}}</mat-error>
<mat-error *ngIf="ctrl.invalid && ctrl.errors?.idAlreadyExists">
{{ validationMessages.idExistsErrorMessage }}
</mat-error>
</mat-form-field>

<!-- Access Policy -->
Expand Down

0 comments on commit dbf324a

Please sign in to comment.