hurl icon indicating copy to clipboard operation
hurl copied to clipboard

Add isIsoDate predicate

Open jcamiel opened this issue 1 year ago • 4 comments

The current isDate predicate takes "date" type as input. To use it on a string, user has to explicitly convert it to a date using a format spec and toDate filter:

POST https://foo.com
HTTP 200
[Asserts]
jsonpath "$.created" toDate "%Y-%m-%dT%H:%M:%S%.f%:z" isDate

As the user must explicitly transform the string to a date, isDate has little value.

Proposal

Add a isIsoDate predicate that take a string as input and check that this string can be converted to a date with the format spec %Y-%m-%dT%H:%M:%S%.f%:z(chrono format).

Like Date.parse and Date.propotype.toISOString JavaScript methods, we're only checking the JavaScript date time string format YYYY-MM-DDTHH:mm:ss.sssZ (simplicification of the ISO 8601)

POST https://foo.com
HTTP 200
[Asserts]
jsonpath "$.created" isIsoDate

jcamiel avatar Feb 14 '24 10:02 jcamiel

I'd like to take this issue, very similar to isDate predicate so I should be able to implement a similar flow.

beaterblank avatar Feb 15 '24 13:02 beaterblank

NaiveDateTime::parse_from_str with %Y-%m-%dT%H:%M:%S%.f%:z" does not work for some reason but removing the % after f makes it work.

what about the time format %Y-%m-%dT%H:%M:%S%.fZ ? should we also consider this as an iso date while testing?

I was thinking of using this high level logic :

use chrono::NaiveDateTime;

fn main() {
    const DATE_FMTS : [&str;2] = ["%Y-%m-%dT%H:%M:%S%.f:z","%Y-%m-%dT%H:%M:%S%.fZ"];
    for fmt in DATE_FMTS{
        if let Ok(_) = NaiveDateTime::parse_from_str(" 2024-02-15T19:47:46.266:z",fmt){
            println!("matched with {}",fmt);
        }
    }
}

Also, I've used ISO string as the expected value type for this is this okay should I just keep it string ?

beaterblank avatar Feb 19 '24 04:02 beaterblank

Please check the PR for above things and let me know if any feedback. Thank you!

beaterblank avatar Feb 19 '24 04:02 beaterblank

Hi @beaterblank I'll make feedback asap

jcamiel avatar Feb 19 '24 17:02 jcamiel

Hi @beaterblank this has been fixed by #2565. I'm. closing this one now

jcamiel avatar Mar 21 '24 19:03 jcamiel