Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added file last modified validator #2096

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param {Date} startDate - The start date of the allowed range.
* @param {Date} minDate - The start date of the allowed range.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this inline with other date validators

* @param {Date} endDate - The end date of the allowed range.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param {Date} endDate - The end date of the allowed range.
* @param {Date} maxDate - 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
Loading