GithubHelp home page GithubHelp logo

isNotEmpty() on date objects about vest HOT 3 CLOSED

ealush avatar ealush commented on May 23, 2024 1
isNotEmpty() on date objects

from vest.

Comments (3)

ealush avatar ealush commented on May 23, 2024 1

Hey @zeekrey, thanks for reaching out!

Yes, isEmpty and isNotEmpty indeed only perform very specific tasks. https://github.com/ealush/vest/blob/latest/packages/n4s/src/rules/isEmpty/index.js, which is not very suitable for checking a date object.

The "empty" Date object that you showed in your code snipped is not empty, since new Date() defaults to the current time:

new Date().valueOf()
// -> 1596463051635

I believe that what you are looking for is a Date-Object-validity validation rule:

new Date('rr')
// -> Invalid Date

new Date('NaN')
// -> Invalid Date

Which can basically be done with isNaN():

isNaN(new Date('x'))
// -> true

isNaN(new Date())
// -> false

Which is an actually good rule to have in Vest. I will add it to the next minor version (2.1).

But in the meantime, you can do either of the following:

 // dateObject = new Date('zzz');

 test('date', 'Just testing a date object.', () => {
	return isNaN(dateObject); // Vest treats `false` as test failure
  });

  // See https://ealush.com/vest/#/./test?id=explicitly-returning-false for more info

Or, if you want to explicitly use enforce, you can extend enforce to add it locally:

enforce.extend({
    isNaN: (value) => isNaN(value)
});

 // dateObject = new Date('zzz');

 test('date', 'Just testing a date object.', () => {
	enforce(dateObject).isNaN();
  });

from vest.

zeekrey avatar zeekrey commented on May 23, 2024 1

Awesome! Thanks for the detailed answer. 😇 I'll go with:

enforce.extend({
isNaN: (value) => isNaN(value)
});

// dateObject = new Date('zzz');

test('date', 'Just testing a date object.', () => {
enforce(dateObject).isNaN();
});

from vest.

ealush avatar ealush commented on May 23, 2024

Glad I was able to help 😀

from vest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.