From d6e5aa012e26994774361543217060a21a18677e Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Wed, 22 Jan 2025 18:05:33 -0600 Subject: [PATCH] Validate date time fields when adding/editing photos --- __tests__/date.test.ts | 44 +++++++++++++++++++-------- src/components/FieldSetWithStatus.tsx | 2 +- src/photo/form/index.ts | 12 ++++++-- src/utility/date.ts | 41 +++++++++++++++++++------ 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/__tests__/date.test.ts b/__tests__/date.test.ts index 6257779e..a6ebadeb 100644 --- a/__tests__/date.test.ts +++ b/__tests__/date.test.ts @@ -1,6 +1,9 @@ +/* eslint-disable max-len */ import { convertTimestampToNaivePostgresString, convertTimestampWithOffsetToPostgresString, + validatePostgresDateString, + validateNaivePostgresDateString, } from '../src/utility/date'; describe('Date utility', () => { @@ -29,19 +32,34 @@ describe('Date utility', () => { expect(convertTimestampToNaivePostgresString(timestamp)) .toBe('2023-12-02 16:38:36'); }); + it('Malformed date string', () => { + const timestamp = '2024/01a/01 Z'; + expect(convertTimestampWithOffsetToPostgresString(timestamp)) + .toBe(convertTimestampWithOffsetToPostgresString( + new Date().toISOString(), + )); + }); + it('Empty string', () => { + const timestamp = ' '; + expect(convertTimestampWithOffsetToPostgresString(timestamp)) + .toBe(convertTimestampWithOffsetToPostgresString( + new Date().toISOString(), + )); + }); }); - it('Malformed date string', () => { - const timestamp = '2024/01a/01 Z'; - expect(convertTimestampWithOffsetToPostgresString(timestamp)) - .toBe(convertTimestampWithOffsetToPostgresString( - new Date().toISOString(), - )); - }); - it('Empty string', () => { - const timestamp = ' '; - expect(convertTimestampWithOffsetToPostgresString(timestamp)) - .toBe(convertTimestampWithOffsetToPostgresString( - new Date().toISOString(), - )); + describe('validates date strings', () => { + it('Correct', () => { + expect(validatePostgresDateString('2025-01-03T21:00:44.000Z')).toBe(true); + expect(validateNaivePostgresDateString('2025-01-03 16:00:44')).toBe(true); + }); + it('Incorrect', () => { + expect(validatePostgresDateString('2024-01-01')).toBe(false); + expect(validatePostgresDateString('2025-01-03 16:00:44')).toBe(false); + expect(validateNaivePostgresDateString('2024-01-01')).toBe(false); + expect(validatePostgresDateString('2025-01-03T21:00:44.000')).toBe(false); + expect(validateNaivePostgresDateString('2025-01-03T16:00:44')).toBe(false); + expect(validatePostgresDateString('2025-01-03T21:00:44.000ZZ')).toBe(false); + expect(validateNaivePostgresDateString('2025-01-03 16:00:44Z')).toBe(false); + }); }); }); diff --git a/src/components/FieldSetWithStatus.tsx b/src/components/FieldSetWithStatus.tsx index 5b0eda42..305ba493 100644 --- a/src/components/FieldSetWithStatus.tsx +++ b/src/components/FieldSetWithStatus.tsx @@ -58,7 +58,7 @@ export default function FieldSetWithStatus({ {!hideLabel && label &&