mirror of https://github.com/ghostfolio/ghostfolio
7 changed files with 72 additions and 63 deletions
@ -1,35 +0,0 @@ |
|||||
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); |
|
||||
}); |
|
||||
|
|
||||
it('Leaves a value which is not a string to IsISO8601', () => { |
|
||||
expect(constraint.validate(undefined)).toBe(true); |
|
||||
expect(constraint.validate(null)).toBe(true); |
|
||||
expect(constraint.validate(123)).toBe(true); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,26 +0,0 @@ |
|||||
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: unknown) { |
|
||||
// IsISO8601 refuses a value which is not a string
|
|
||||
if (typeof aDate !== 'string') { |
|
||||
return true; |
|
||||
} |
|
||||
|
|
||||
return !isAccountBalanceInFuture({ date: resetHours(parseISO(aDate)) }); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,11 @@ |
|||||
|
import { IsValidAccountBalanceDateConstraint } from './is-valid-account-balance-date'; |
||||
|
|
||||
|
describe('IsValidAccountBalanceDateConstraint', () => { |
||||
|
const constraint = new IsValidAccountBalanceDateConstraint(); |
||||
|
|
||||
|
it('Leaves a value which is not a string to IsISO8601', () => { |
||||
|
expect(constraint.validate(undefined)).toBe(true); |
||||
|
expect(constraint.validate(null)).toBe(true); |
||||
|
expect(constraint.validate(123)).toBe(true); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,22 @@ |
|||||
|
import { isValidAccountBalanceDate } from '@ghostfolio/common/helper'; |
||||
|
|
||||
|
import { |
||||
|
ValidatorConstraint, |
||||
|
ValidatorConstraintInterface |
||||
|
} from 'class-validator'; |
||||
|
|
||||
|
@ValidatorConstraint({ name: 'isValidAccountBalanceDate' }) |
||||
|
export class IsValidAccountBalanceDateConstraint implements ValidatorConstraintInterface { |
||||
|
public defaultMessage() { |
||||
|
return '$property must be a valid date which is not in the future'; |
||||
|
} |
||||
|
|
||||
|
public validate(aDate: unknown) { |
||||
|
// IsISO8601 refuses a value which is not a string
|
||||
|
if (typeof aDate !== 'string') { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
return isValidAccountBalanceDate(aDate); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue