mirror of https://github.com/ghostfolio/ghostfolio
10 changed files with 110 additions and 52 deletions
@ -0,0 +1,29 @@ |
|||||
|
import { IsNotInTheFutureConstraint } from './is-not-in-the-future'; |
||||
|
|
||||
|
describe('IsNotInTheFutureConstraint', () => { |
||||
|
const constraint = new IsNotInTheFutureConstraint(); |
||||
|
|
||||
|
beforeAll(() => { |
||||
|
jest.useFakeTimers().setSystemTime(new Date('2026-09-03T23:30:00.000Z')); |
||||
|
}); |
||||
|
|
||||
|
afterAll(() => { |
||||
|
jest.useRealTimers(); |
||||
|
}); |
||||
|
|
||||
|
it('Accepts a date in the past', () => { |
||||
|
expect(constraint.validate('2026-09-02')).toBe(true); |
||||
|
}); |
||||
|
|
||||
|
it('Accepts the current date', () => { |
||||
|
expect(constraint.validate('2026-09-03')).toBe(true); |
||||
|
}); |
||||
|
|
||||
|
it('Accepts the current date of a user in a time zone ahead of the instance', () => { |
||||
|
expect(constraint.validate('2026-09-04')).toBe(true); |
||||
|
}); |
||||
|
|
||||
|
it('Refuses a date in the future', () => { |
||||
|
expect(constraint.validate('2026-09-05')).toBe(false); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,21 @@ |
|||||
|
import { |
||||
|
isAccountBalanceInFuture, |
||||
|
resetHours |
||||
|
} from '@ghostfolio/common/helper'; |
||||
|
|
||||
|
import { |
||||
|
ValidatorConstraint, |
||||
|
ValidatorConstraintInterface |
||||
|
} from 'class-validator'; |
||||
|
import { parseISO } from 'date-fns'; |
||||
|
|
||||
|
@ValidatorConstraint({ name: 'isNotInTheFuture' }) |
||||
|
export class IsNotInTheFutureConstraint implements ValidatorConstraintInterface { |
||||
|
public defaultMessage() { |
||||
|
return '$property must not be a date in the future'; |
||||
|
} |
||||
|
|
||||
|
public validate(aDate: string) { |
||||
|
return !isAccountBalanceInFuture({ date: resetHours(parseISO(aDate)) }); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue