Skip to content

Commit 7c9a76c

Browse files
P-Jeremybpetetot
authored andcommitted
feat(api): add isoDateParser for scripts
1 parent bf61c8f commit 7c9a76c

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

api/scripts/legal-documents/add-new-legal-document-version.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'dotenv/config';
22

3-
import Joi from 'joi';
4-
53
import { usecases } from '../../src/legal-documents/domain/usecases/index.js';
4+
import { isoDateParser } from '../../src/shared/application/scripts/parsers.js';
65
import { Script } from '../../src/shared/application/scripts/script.js';
76
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js';
87

@@ -29,16 +28,7 @@ export class AddNewLegalDocumentVersion extends Script {
2928
describe: 'Version date of the legal document, format "YYYY-MM-DD", (ex: "2020-02-27")',
3029
demandOption: true,
3130
requiresArg: true,
32-
coerce: (value) => {
33-
const schema = Joi.string()
34-
.pattern(/^\d{4}-\d{2}-\d{2}$/)
35-
.message('Invalid date format. Expected "YYYY-MM-DD".');
36-
const { error, value: validatedDate } = schema.validate(value);
37-
if (error) {
38-
throw new Error(error.message);
39-
}
40-
return new Date(validatedDate);
41-
},
31+
coerce: isoDateParser(),
4232
},
4333
},
4434
});

api/src/shared/application/scripts/parsers.js

+18
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,21 @@ export function commaSeparatedNumberParser(separator = ',') {
5050
return Joi.attempt(data, Joi.array().items(Joi.number()));
5151
};
5252
}
53+
54+
/**
55+
* Create a parser for date strings in the format "YYYY-MM-DD"
56+
* @returns {Date}
57+
*/
58+
export function isoDateParser() {
59+
return (date) => {
60+
const schema = Joi.string()
61+
.pattern(/^\d{4}-\d{2}-\d{2}$/)
62+
.message('Invalid date format. Expected "YYYY-MM-DD".');
63+
64+
const { error, value: validatedDate } = schema.validate(date);
65+
if (error) {
66+
throw new Error(error.message);
67+
}
68+
return new Date(validatedDate);
69+
};
70+
}

api/tests/shared/unit/application/scripts/parsers_test.js

+42
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
commaSeparatedNumberParser,
77
commaSeparatedStringParser,
88
csvFileParser,
9+
isoDateParser,
910
} from '../../../../../src/shared/application/scripts/parsers.js';
1011
import { catchErr, expect } from '../../../../test-helper.js';
1112

@@ -108,4 +109,45 @@ describe('Shared | Unit | Application | Parsers', function () {
108109
expect(() => parser(input)).to.throw('"[1]" must be a number');
109110
});
110111
});
112+
113+
describe('isoDateParser', function () {
114+
it('parses a valid date string in YYYY-MM-DD format', function () {
115+
// given
116+
const input = '2023-12-25';
117+
118+
// when
119+
const parser = isoDateParser();
120+
const result = parser(input);
121+
122+
// then
123+
expect(result).to.be.instanceOf(Date);
124+
expect(result.toISOString().startsWith('2023-12-25')).to.be.true;
125+
});
126+
127+
it('throws an error for an invalid date format', async function () {
128+
// given
129+
const input = '12-25-2023';
130+
131+
// when
132+
const parser = isoDateParser();
133+
const error = await catchErr(parser)(input);
134+
135+
// then
136+
expect(error).to.be.instanceOf(Error);
137+
expect(error.message).to.equal('Invalid date format. Expected "YYYY-MM-DD".');
138+
});
139+
140+
it('throws an error for non-date strings', async function () {
141+
// given
142+
const input = 'not-a-date';
143+
144+
// when
145+
const parser = isoDateParser();
146+
const error = await catchErr(parser)(input);
147+
148+
// then
149+
expect(error).to.be.instanceOf(Error);
150+
expect(error.message).to.equal('Invalid date format. Expected "YYYY-MM-DD".');
151+
});
152+
});
111153
});

0 commit comments

Comments
 (0)