-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e8e417
commit 905bedf
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/ui/components/form-core/src/validate/FileLastModifiedDateValidator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/ui/components/form-core/test/validate/FileLastModifiedDateValidator.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters