Feature/improve date validation in activity endpoints (#3489)

* Improve date validation

* Update changelog
pull/3491/head
Thomas Kaul 5 months ago committed by GitHub
parent 0adefe14e1
commit bf20a5de82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Improved the date validation in the create, import and update activities endpoints
- Improved the language localization for German (`de`) - Improved the language localization for German (`de`)
## 2.88.0 - 2024-06-11 ## 2.88.0 - 2024-06-11

@ -1,3 +1,5 @@
import { IsAfter1970Constraint } from '@ghostfolio/common/validator-constraints/is-after-1970';
import { import {
AssetClass, AssetClass,
AssetSubClass, AssetSubClass,
@ -15,7 +17,8 @@ import {
IsNumber, IsNumber,
IsOptional, IsOptional,
IsString, IsString,
Min Min,
Validate
} from 'class-validator'; } from 'class-validator';
import { isString } from 'lodash'; import { isString } from 'lodash';
@ -51,6 +54,7 @@ export class CreateOrderDto {
dataSource?: DataSource; dataSource?: DataSource;
@IsISO8601() @IsISO8601()
@Validate(IsAfter1970Constraint)
date: string; date: string;
@IsNumber() @IsNumber()

@ -1,3 +1,5 @@
import { IsAfter1970Constraint } from '@ghostfolio/common/validator-constraints/is-after-1970';
import { import {
AssetClass, AssetClass,
AssetSubClass, AssetSubClass,
@ -14,7 +16,8 @@ import {
IsNumber, IsNumber,
IsOptional, IsOptional,
IsString, IsString,
Min Min,
Validate
} from 'class-validator'; } from 'class-validator';
import { isString } from 'lodash'; import { isString } from 'lodash';
@ -49,6 +52,7 @@ export class UpdateOrderDto {
dataSource: DataSource; dataSource: DataSource;
@IsISO8601() @IsISO8601()
@Validate(IsAfter1970Constraint)
date: string; date: string;
@IsNumber() @IsNumber()

@ -21,7 +21,7 @@ import { DateAdapter, MAT_DATE_LOCALE } from '@angular/material/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { AssetClass, AssetSubClass, Tag, Type } from '@prisma/client'; import { AssetClass, AssetSubClass, Tag, Type } from '@prisma/client';
import { isUUID } from 'class-validator'; import { isUUID } from 'class-validator';
import { isToday } from 'date-fns'; import { isAfter, isToday } from 'date-fns';
import { EMPTY, Observable, Subject, lastValueFrom, of } from 'rxjs'; import { EMPTY, Observable, Subject, lastValueFrom, of } from 'rxjs';
import { catchError, delay, map, startWith, takeUntil } from 'rxjs/operators'; import { catchError, delay, map, startWith, takeUntil } from 'rxjs/operators';
@ -426,6 +426,14 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
}); });
} }
public dateFilter(aDate: Date) {
if (!aDate) {
return true;
}
return isAfter(aDate, new Date(0));
}
public onAddTag(event: MatAutocompleteSelectedEvent) { public onAddTag(event: MatAutocompleteSelectedEvent) {
this.activityForm.get('tags').setValue([ this.activityForm.get('tags').setValue([
...(this.activityForm.get('tags').value ?? []), ...(this.activityForm.get('tags').value ?? []),

@ -157,7 +157,12 @@
<div class="mb-3"> <div class="mb-3">
<mat-form-field appearance="outline" class="w-100"> <mat-form-field appearance="outline" class="w-100">
<mat-label i18n>Date</mat-label> <mat-label i18n>Date</mat-label>
<input formControlName="date" matInput [matDatepicker]="date" /> <input
formControlName="date"
matInput
[matDatepicker]="date"
[matDatepickerFilter]="dateFilter"
/>
<mat-datepicker-toggle class="mr-2" matSuffix [for]="date"> <mat-datepicker-toggle class="mr-2" matSuffix [for]="date">
<ion-icon <ion-icon
class="text-muted" class="text-muted"

@ -0,0 +1,16 @@
import {
ValidatorConstraint,
ValidatorConstraintInterface
} from 'class-validator';
import { format, isAfter, parseISO } from 'date-fns';
@ValidatorConstraint({ name: 'isAfter1970' })
export class IsAfter1970Constraint implements ValidatorConstraintInterface {
public defaultMessage() {
return `date must be after ${format(new Date(0), 'yyyy')}`;
}
public validate(aDateString: string) {
return isAfter(parseISO(aDateString), new Date(0));
}
}

@ -0,0 +1,18 @@
{
"meta": {
"date": "2021-01-01T00:00:00.000Z",
"version": "dev"
},
"activities": [
{
"currency": "USD",
"dataSource": "YAHOO",
"date": "1960-01-01T00:00:00.000Z",
"fee": 0,
"quantity": 20,
"symbol": "AAPL",
"type": "BUY",
"unitPrice": 100.0
}
]
}
Loading…
Cancel
Save