Skip to content

Commit a7b63ce

Browse files
committed
feat: add DateTime validation functions and include validator dependency
1 parent f974245 commit a7b63ce

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ Checks:
8080
- maybeDate
8181
- asDate
8282
- maybeAsDate
83+
- DateTime
84+
- isDateTime
85+
- maybeDateTime
86+
- asDateTime
87+
- maybeAsDateTime
8388
- Enum
8489
- isEnum
8590
- maybeEnum
@@ -242,6 +247,31 @@ Options:
242247
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error
243248
- `validatorOptions?: any` - options to pass to the _validator_
244249

250+
### `isDateTime`, `maybeDateTime`, `asDateTime`, `maybeAsDateTime`
251+
252+
Similar to above but for luxon DateTime.
253+
254+
> NOTE: Requires peer dependencies `luxon` and `@types/luxon`.
255+
256+
Usage:
257+
258+
```typescript
259+
isDateTime(options);
260+
maybeDateTime(options);
261+
asDateTime(options);
262+
maybeAsDateTime(options);
263+
```
264+
265+
Options:
266+
267+
- `converter?: (value: unknown, options?: any) => T | undefined` - custom converter function, if not defined or `undefined` is returned then built in conversions will be run
268+
- `convertOptions` - options to pass to the _converter_
269+
- `format` - custom date format used in conversion from `string` to `Date` see [Luxon formatting](https://moment.github.io/luxon/docs/manual/formatting)
270+
- `maxFuture?: Duration` - if the value is after this duration into the future, it's an error `max-future`
271+
- `maxPast?: Duration` - if the value is before this duration into the past, it's an error `max-past`
272+
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error
273+
- `validatorOptions?: any` - options to pass to the _validator_
274+
245275
### `isEnum`, `maybeEnum`, `asEnum`, `maybeAsEnum`
246276

247277
Usage:
@@ -399,6 +429,7 @@ StringFormat:
399429
- `StringFormat.ULID()` - https://github.com/ulid/spec
400430
- `StringFormat.UUID()` - https://www.ietf.org/rfc/rfc4122.txt
401431
- `StringFormat.password(requirements: PasswordRequirements)` - Password format with minimum requirements
432+
- `StringFormat.email(options: { allowDisplayName: boolean })` - requires peer dependency `validator`
402433

403434
Example:
404435

@@ -427,6 +458,12 @@ const check = isString({
427458
});
428459
```
429460

461+
```typescript
462+
const check = isString({
463+
format: StringFormat.email({ allowDisplayName: false }),
464+
});
465+
```
466+
430467
```typescript
431468
// change case
432469
import { pascalCase } from 'change-case';

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"json-date-parser": "^1.0.1",
5252
"ts-jest": "^29.4.0",
5353
"tslint": "^6.1.3",
54-
"typescript": "^5.8.3"
54+
"typescript": "^5.8.3",
55+
"validator": "^13.15.15"
5556
},
5657
"peerDependencies": {
5758
"@types/luxon": "^3.6.2",

0 commit comments

Comments
 (0)