Skip to content

NIFI-15674: Add isValidDate and isValidInstant Expression Language fu…#10975

Open
Scrooge-McDucks wants to merge 1 commit intoapache:mainfrom
Scrooge-McDucks:NIFI-15674
Open

NIFI-15674: Add isValidDate and isValidInstant Expression Language fu…#10975
Scrooge-McDucks wants to merge 1 commit intoapache:mainfrom
Scrooge-McDucks:NIFI-15674

Conversation

@Scrooge-McDucks
Copy link
Contributor

Summary

NIFI-15674

Adds two new Expression Language validation functions:

  • isValidDate(format[, timezone])
  • isValidInstant()

These functions allow flows to validate date and instant values before attempting conversion.

Motivation

NiFi currently does not provide a simple top-level Expression Language function for validating whether a value can be parsed as a date or instant.

Today, users often need to rely on regex, nested expressions, or extra processor logic in places like UpdateAttribute or RouteOnAttribute to guard parsing. Regex can validate shape, but it is not calendar-aware, so values like 31-02-2026 may still appear valid even though they are not real dates.

This also creates operational risk. If invalid values are parsed directly, expression evaluation can fail, which in some flows can lead to repeated retries or queue looping instead of clean handling.

These functions provide a lightweight and safe way to keep validation logic visible at the top level of an expression.

Behavior

isValidDate(format)

Returns true if the subject can be parsed as a valid date using the supplied format, otherwise false.

isValidDate(format, timezone)

Returns true if the subject can be parsed as a valid date using the supplied format and timezone, otherwise false.

isValidInstant()

Returns true if the subject can be parsed as a valid ISO-8601 instant, otherwise false.

Invalid input returns false rather than throwing an evaluation error.

Examples

${myDate:isValidDate("dd-MM-yyyy"):ifElse("valid","invalid")}

${myDate:isValidDate("yyyy-MM-dd HH:mm:ss", "UTC")}

${eventTime:isValidInstant():ifElse("valid-instant","invalid-instant")}

Tracking

Please complete the following tracking steps prior to pull request creation.

Issue Tracking

Pull Request Tracking

  • Pull Request title starts with Apache NiFi Jira issue number, such as NIFI-00000
  • Pull Request commit message starts with Apache NiFi Jira issue number, as such NIFI-00000
  • Pull request contains commits signed with a registered key indicating Verified status

Pull Request Formatting

  • Pull Request based on current revision of the main branch
  • Pull Request refers to a feature branch with one commit containing changes

Verification

Please indicate the verification steps performed prior to pull request creation.

Build

  • Build completed using ./mvnw clean install -P contrib-check
    • JDK 21
    • JDK 25

Licensing

  • New dependencies are compatible with the Apache License 2.0 according to the License Policy
  • New dependencies are documented in applicable LICENSE and NOTICE files

Documentation

  • Documentation formatting appears as expected in rendered files

@Scrooge-McDucks
Copy link
Contributor Author

Just a gentle nudge on this one when anyone has a chance!

booleanFunctionRef : zeroArgBool | oneArgBool | multiArgBool | oneOrTwoArgBool;
numberFunctionRef : zeroArgNum | oneArgNum | zeroOrTwoArgNum | oneOrTwoArgNum | zeroOrOneOrTwoArgNum;

anyArg : WHOLE_NUMBER | DECIMAL | numberFunctionRef | STRING_LITERAL | zeroArgString | oneArgString | twoArgString | fiveArgString | booleanLiteral | zeroArgBool | oneArgBool | multiArgBool
Copy link
Contributor

Choose a reason for hiding this comment

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

oneOrTwoArgBool needs to be added there


// Use parseUnresolved to get raw field values without lenient resolution
final ParsePosition pos = new ParsePosition(0);
final TemporalAccessor parsed = dtf.parseUnresolved(subjectValue, pos);
Copy link
Contributor

Choose a reason for hiding this comment

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

You are not using trim() here while you are in the other class. Is it on purpose?

INSTANT_FORMATTER.parse(subjectValue.trim(), Instant::from);

final ZoneId zone = dtf.getZone();
zoneRules = (zone != null && !(zone instanceof ZoneOffset)) ? zone.getRules() : null;
}
if (zoneRules != null && month != -1 && parsed.isSupported(ChronoField.HOUR_OF_DAY)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The DST gap check only activates when ChronoField.HOUR_OF_DAY is present in the parsed result. Format patterns using 12-hour time with AM/PM markers (e.g., hh:mm a) produce HOUR_OF_AMPM and AMPM_OF_DAY fields instead of HOUR_OF_DAY, so DST gap validation would be silently skipped for those formats. This is a minor edge case but worth documenting or addressing.

@Scrooge-McDucks
Copy link
Contributor Author

Thanks for the review @pvillard31! Fixed all three - the grammar was an oversight and trim were an oversight.

the 12-hour clock edge case was a good catch, and one I didn't think of! Added tests for the DST gap with hh:mm a format too.

…nctions

- isValidDate(format[, timezone]) returns true if the subject parses as
  a valid calendar date using the given format and optional timezone;
  rejects invalid calendar combinations (e.g. Feb 31), DST gap times,
  and supports both 24-hour (HH) and 12-hour (hh a) clock formats
- isValidInstant() returns true if the subject parses as a valid
  ISO-8601 instant (ISO_INSTANT, ISO_OFFSET_DATE_TIME, RFC_1123)
- Added oneOrTwoArgBool to anyArg in parser grammar so isValidDate
  can appear as an argument to other functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants