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
- Improved the date validation in the create, import and update activities endpoints
- Improved the language localization for German (`de`)
## 2.88.0 - 2024-06-11

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

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

@ -21,7 +21,7 @@ import { DateAdapter, MAT_DATE_LOCALE } from '@angular/material/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { AssetClass, AssetSubClass, Tag, Type } from '@prisma/client';
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 { 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) {
this.activityForm.get('tags').setValue([
...(this.activityForm.get('tags').value ?? []),

@ -157,7 +157,12 @@
<div class="mb-3">
<mat-form-field appearance="outline" class="w-100">
<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">
<ion-icon
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