Skip to content

Commit

Permalink
added file last modified validator
Browse files Browse the repository at this point in the history
  • Loading branch information
labibismaiel committed Oct 16, 2023
1 parent 5e8e417 commit 905bedf
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Validator } from './Validator.js';

export class FileLastModifiedDateValidator extends Validator {
static validatorName = 'FileLastModifiedDate';

/**
* Checks if the file's last modified date is within the specified range.
* @param {Date} fileLastModifiedDate - The file's last modified date.
* @param {Date} startDate - The start date of the allowed range.
* @param {Date} endDate - The end date of the allowed range.
* @returns {boolean}
*/
static isDateWithinRange(fileLastModifiedDate, startDate, endDate) {
return fileLastModifiedDate >= startDate && fileLastModifiedDate <= endDate;
}

/**
* @param {Array<File>} modelValue - Array of file objects.
* @param {{ startDate: Date; endDate: Date; }} params - Validation parameters.
* @returns {boolean}
*/
execute(modelValue, params = this.param) {
const ctor = /** @type {typeof FileLastModifiedDateValidator} */ (this.constructor);
return modelValue.every(file => {
const fileDate = new Date(file.lastModified);
return ctor.isDateWithinRange(fileDate, params.startDate, params.endDate);
});
}

static async getMessage() {
return 'The file must have been last modified within the specified date range.';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from '@open-wc/testing';
import { FileLastModifiedDateValidator } from '@lion/ui/FileLastModifiedDateValidator';

describe('FileLastModifiedDateValidator', () => {
let validator;

beforeEach(() => {
validator = new FileLastModifiedDateValidator({
startDate: new Date('2022-01-01'),
endDate: new Date('2023-01-01'),
});
});

it('validates files within the specified date range', () => {
const validFile = new File(['content'], 'test.txt', {
type: 'text/plain',
lastModified: new Date('2022-06-01').getTime(),
});

const result = validator.execute([validFile]);
expect(result).to.be.true;
});

it('invalidates files outside the specified date range', () => {
const invalidFile = new File(['content'], 'test.txt', {
type: 'text/plain',
lastModified: new Date('2021-12-31').getTime(),
});

const result = validator.execute([invalidFile]);
expect(result).to.be.false;
});

it('returns a validation message for invalid files', async () => {
const message = await FileLastModifiedDateValidator.getMessage();
expect(message).to.equal(
'The file must have been last modified within the specified date range.',
);
});
});
1 change: 1 addition & 0 deletions packages/ui/exports/form-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
export { DefaultSuccess } from '../components/form-core/src/validate/resultValidators/DefaultSuccess.js';

export { LionValidationFeedback } from '../components/form-core/src/validate/LionValidationFeedback.js';
export { FileLastModifiedDateValidator } from '../components/form-core/src/validate/FileLastModifiedDateValidator.js';

export { ChoiceGroupMixin } from '../components/form-core/src/choice-group/ChoiceGroupMixin.js';
export { ChoiceInputMixin } from '../components/form-core/src/choice-group/ChoiceInputMixin.js';
Expand Down

0 comments on commit 905bedf

Please sign in to comment.