validatorjs / validator.js

String validation
MIT License
23.04k stars 2.3k forks source link

validator.js timestamp validation support #2300

Closed Ryrahul closed 1 year ago

Ryrahul commented 1 year ago

Issue

Describe the bug

validator.js does not currently have a built-in feature for validating timestamps.

Examples


const validator = new validator();

validator.add('timestamp', {
  type: 'timestamp',
  message: 'Please enter a valid timestamp.'
});

validator.validate({
  timestamp: 1656480000 // This is a valid timestamp.
}); // Always returns false
shishiro26 commented 1 year ago

I would like to work on this under hacktober. can u assign this to me

Ryrahul commented 1 year ago

Thank you for your interest in working on this issue! I appreciate your enthusiasm. However, I suggest we wait for the repository maintainer to have a look at the issue first. Once they provide their input, we can collaborate on resolving it together. This way, we can ensure that any changes made align with the project's goals and guidelines. Thanks again for your willingness to contribute!

shishiro26 commented 1 year ago

Thank you for getting back to me. I'm more than happy to wait for the repository maintainer's input and collaborate on resolving the issue together. Please let me know if there's anything specific you would like me to do in the meantime. Thanks again for the opportunity to be a part of this project!

WikiRik commented 1 year ago

I'm not familiar with this syntax, are you sure you are making an issue for this project?

Ryrahul commented 1 year ago

Hi @WikiRik

Thank you for your reply. I understand that you are not familiar with the syntax I am using to add a timestamp validator. I apologize for any confusion.

I am proposing a change to validator.js to add a default timestamp validator. I believe that this would be a valuable improvement for the library, as it would make it easier for users to validate timestamps.


validator.register('timestamp', {
  validate(timestamp) {
    // Check if the timestamp is a number.
    if (!timestamp || isNaN(timestamp)) {
      return false;
    }

    // Check if the timestamp is within a certain range.
    const minTimestamp = 1656480000; // Unix timestamp for 2023-10-01 00:00:00 UTC
    const maxTimestamp = Date.now(); // Current Unix timestamp
    if (timestamp < minTimestamp || timestamp > maxTimestamp) {
      return false;
    }

    return true;
  }});

 I believe that this implementation would be simple to implement and easy to use. It would also be flexible enough to meet the needs of most users.

 would appreciate it if you would consider adding this change to validator.js.

 Thank you for your time and consideration.