6.1.0 • Published 9 months ago

@finnair/v-validation-luxon v6.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

npm version

v-validation-luxon

@finnair/v-validation-luxon is an extension to @finnair/v-validation.

Vluxon extension provides custom wrapper types for Luxon DateTime to support full JSON roundtrip with strict validation. Also plain DateTime validators are provided and custom formats are easy to define (see the code for examples).

Documentation for v-validation.

Getting Started

Install v-validation using yarn:

yarn add @finnair/v-validation-luxon

Or npm:

npm install @finnair/v-validation-luxon

Vluxon

Vluxon contains both plain DateTime validators and also validators that return DateTime wrappers that guarantee some normalizations (zone, date and/or time) and especially JSON serialization in the given format. The wrappers are immutable and they allow easy access to the DateTime instance for further processing. validateLuxon function can be used to build custom DateTime validators/converters by supplying a RegExp pattern and a parser function.

Supported DateTime Wrapper Types

ClassDescription
LuxonDateTimeAbstract base class for the wrappers.
LocalDateLuxonInput and JSON output in yyyy-MM-dd format. Time normalized to midnight UTC.
LocalTimeLuxonInput and JSON output in HH:mm:ss format. Date normalized to 1970-01-01 UTC (Unix Epoch).
DateTimeLuxonInput and JSON output in yyyy-MM-ddTHH:mm:ssZ format in local/given/parsed zone with milliseconds zeroed.
DateTimeUtcLuxonInput and JSON output in yyyy-MM-ddTHH:mm:ssZ format in UTC zone with milliseconds zeroed.
DateTimeMillisLuxonInput and JSON output in yyyy-MM-ddTHH:mm:ss.SSSZ format in local/given/parsed zone.
DateTimeMillisUtcLuxonInput and JSON output in yyyy-MM-ddTHH:mm:ss.SSSZ format in UTC zone.

Constructors

Wrapper types may be constructed with new from DateTime instance, but there are also shortcuts:

Static MethodDescription
now()Current time in DateTime* types.
nowUtc()Current UTC time in Local* types.
nowLocal(options?: DateTimeJSOptions)Current local time in Local* types (defaults to system zone).
fromISO(value: string, options?: DateTimeOptions)Parse from ISO format (see Luxon DateTime.fromISO).
fromFormat(value: string, format: string, options?: DateTimeOptions)Parse from custom format (see Luxon DateTime.fromFormat).
fromJSDate(date: Date, options?: { zone?: string \| Zone })From JavaScript Date object (see Luxon DateTime.fromJSDate).
fromMillis(millis: number, options?: DateTimeJSOptions)From Unix millis timestamp (see Luxon DateTime.fromMillis).

Instance Methods

Wrappers are meant to be as thin as possible with most of the DateTime functionality accessed directly from the wrapped DateTime instance which is public readonly fiedl. However there are a few convenience methods for working with the wrapper types:

MethodDescription
as(type)Conversion to the given type that extends LuxonDateTime. Note that the conversion is not guaranted to be lossless.
wrap(fn: (dateTime: DateTime) => DateTime)Executes the given function on the wrapped DateTime and rewraps the result.
apply<R>(fn: (dateTime: DateTime) => R): RExecutes the given function on the wrapped DateTime and returns the result as it is.
valueOf()Conversion to millis. This allows comparing wrapper types directly using ==, <, >, <= or >=.
equals(other: any)Type-aware equality.
toJSON()Type-specific serialization (string).

Build-in Validators

Vluxon.FormatDescription
localDateyyyy-MM-ddLocal date (time normalized to midninght UTC).
localTimeHH:mm:ssLocal time (date normalized to 1970-01-01).
dateTimeyyyy-MM-ddTHH:mm:ssZDate and time in local (parsed) time zone.
dateTimeUtcyyyy-MM-ddTHH:mm:ssZDate and time in UTC time zone.
dateTimeMillisyyyy-MM-ddTHH:mm:ss.SSSZDate and time with millis in local (parsed) time zone.
dateTimeMillisUtcyyyy-MM-ddTHH:mm:ss.SSSZDate and time with millis in UTC time zone.
dateTimeFromISOAny ISOPlain Luxon DateTime from ISO format.
dateTimeFromRFC2822RFC2822Plain Luxon DateTime from RFC2822 format.
dateTimeFromHTTPHTTP date-timePlain Luxon DateTime from HTTP format.
dateTimeFromSQLSQL date-timePlain Luxon DateTime from SQL format.
dateTimeFromSecondsUnix timestamp (number)Plain Luxon DateTime from Unix timestamp in seconds.
dateTimeFromMillisUnix timestamp (number)Plain Luxon DateTime from Unix timestamp in milliseconds.
durationISO 8601 DurationLuxon Duration.fromISO with pattern validation.
timeDurationISO 8601 time string as DurationLuxon Duration.fromISOTime.
6.1.0

9 months ago

6.0.1

10 months ago

6.0.0

10 months ago

6.0.2

10 months ago

5.4.0

1 year ago

5.3.0

1 year ago

5.2.0

1 year ago

5.1.0

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.3.0

2 years ago

4.2.0

2 years ago

3.2.0

3 years ago

3.1.0

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

3.0.0

3 years ago

3.0.0-alpha.1

3 years ago

3.0.0-alpha.0

3 years ago

3.0.0-alpha.2

3 years ago

2.0.0

3 years ago

1.1.0

3 years ago

1.1.0-alpha.9

3 years ago

1.1.0-alpha.8

3 years ago

1.1.0-alpha.7

3 years ago

1.1.0-alpha.6

3 years ago

1.1.0-alpha.5

3 years ago

1.1.0-alpha.4

3 years ago

1.1.0-alpha.3

3 years ago

1.1.0-alpha.2

3 years ago

1.1.0-alpha.1

3 years ago

1.1.0-alpha.0

3 years ago