Skip to content

Commit

Permalink
feat(api): add isoDateParser for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Jeremy authored and bpetetot committed Dec 13, 2024
1 parent 4da6b8a commit 5cd3feb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
14 changes: 2 additions & 12 deletions api/scripts/legal-documents/add-new-legal-document-version.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dotenv/config';

import Joi from 'joi';

import { usecases } from '../../src/legal-documents/domain/usecases/index.js';
import { isoDateParser } from '../../src/shared/application/scripts/parsers.js';
import { Script } from '../../src/shared/application/scripts/script.js';
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js';

Expand All @@ -29,16 +28,7 @@ export class AddNewLegalDocumentVersion extends Script {
describe: 'Version date of the legal document, format "YYYY-MM-DD", (ex: "2020-02-27")',
demandOption: true,
requiresArg: true,
coerce: (value) => {
const schema = Joi.string()
.pattern(/^\d{4}-\d{2}-\d{2}$/)
.message('Invalid date format. Expected "YYYY-MM-DD".');
const { error, value: validatedDate } = schema.validate(value);
if (error) {
throw new Error(error.message);
}
return new Date(validatedDate);
},
coerce: isoDateParser(),
},
},
});
Expand Down
18 changes: 18 additions & 0 deletions api/src/shared/application/scripts/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,21 @@ export function commaSeparatedNumberParser(separator = ',') {
return Joi.attempt(data, Joi.array().items(Joi.number()));
};
}

/**
* Create a parser for date strings in the format "YYYY-MM-DD"
* @returns {Date}
*/
export function isoDateParser() {
return (date) => {
const schema = Joi.string()
.pattern(/^\d{4}-\d{2}-\d{2}$/)
.message('Invalid date format. Expected "YYYY-MM-DD".');

const { error, value: validatedDate } = schema.validate(date);
if (error) {
throw new Error(error.message);
}
return new Date(validatedDate);
};
}
42 changes: 42 additions & 0 deletions api/tests/shared/unit/application/scripts/parsers_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
commaSeparatedNumberParser,
commaSeparatedStringParser,
csvFileParser,
isoDateParser,
} from '../../../../../src/shared/application/scripts/parsers.js';
import { catchErr, expect } from '../../../../test-helper.js';

Expand Down Expand Up @@ -108,4 +109,45 @@ describe('Shared | Unit | Application | Parsers', function () {
expect(() => parser(input)).to.throw('"[1]" must be a number');
});
});

describe('isoDateParser', function () {
it('parses a valid date string in YYYY-MM-DD format', function () {
// given
const input = '2023-12-25';

// when
const parser = isoDateParser();
const result = parser(input);

// then
expect(result).to.be.instanceOf(Date);
expect(result.toISOString().startsWith('2023-12-25')).to.be.true;
});

it('throws an error for an invalid date format', async function () {
// given
const input = '12-25-2023';

// when
const parser = isoDateParser();
const error = await catchErr(parser)(input);

// then
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Invalid date format. Expected "YYYY-MM-DD".');
});

it('throws an error for non-date strings', async function () {
// given
const input = 'not-a-date';

// when
const parser = isoDateParser();
const error = await catchErr(parser)(input);

// then
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Invalid date format. Expected "YYYY-MM-DD".');
});
});
});

0 comments on commit 5cd3feb

Please sign in to comment.