@codeleap/form 5.4.1 → 5.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/form",
3
- "version": "5.4.1",
3
+ "version": "5.4.2",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -9,9 +9,9 @@
9
9
  "directory": "packages/form"
10
10
  },
11
11
  "devDependencies": {
12
- "@codeleap/config": "5.4.1",
13
- "@codeleap/types": "5.4.1",
14
- "@codeleap/store": "5.4.1",
12
+ "@codeleap/config": "5.4.2",
13
+ "@codeleap/types": "5.4.2",
14
+ "@codeleap/store": "5.4.2",
15
15
  "zod": "3.23.8",
16
16
  "ts-node-dev": "1.1.8"
17
17
  },
@@ -20,9 +20,9 @@
20
20
  "playground": "bun src/test.ts"
21
21
  },
22
22
  "peerDependencies": {
23
- "@codeleap/types": "5.4.1",
24
- "@codeleap/store": "5.4.1",
25
- "@codeleap/logger": "5.4.1",
23
+ "@codeleap/types": "5.4.2",
24
+ "@codeleap/store": "5.4.2",
25
+ "@codeleap/logger": "5.4.2",
26
26
  "zod": "*",
27
27
  "react": "18.2.0",
28
28
  "typescript": "5.5.2",
package/package.json.bak CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/form",
3
- "version": "5.4.1",
3
+ "version": "5.4.2",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -2,6 +2,7 @@ import { Field } from "../lib/Field"
2
2
  import { z } from 'zod'
3
3
  import { FieldOptions, Validator } from "../types"
4
4
  import { zodValidator } from "../validators"
5
+ import dayjs from 'dayjs'
5
6
 
6
7
  export type DateValidator<R = any, Err = any> = Validator<Date, R, Err>
7
8
 
@@ -10,13 +11,42 @@ type DateFieldOptions<Validate extends DateValidator> = FieldOptions<Date, Valid
10
11
  maximumDate?: Date
11
12
  }
12
13
 
14
+ function normalizeToDate(val: unknown): Date | undefined {
15
+ if (!val) return undefined
16
+
17
+ if (val instanceof Date) return val
18
+ if (dayjs.isDayjs(val)) return val.startOf('day').toDate()
19
+ if (typeof val === 'string') {
20
+ const parsed = dayjs(val)
21
+ return parsed.isValid() ? parsed.startOf('day').toDate() : undefined
22
+ }
23
+
24
+ return undefined
25
+ }
26
+
13
27
  export class DateField<Validate extends DateValidator> extends Field<Date, Validate> {
14
28
  _type = "DATE"
15
29
 
16
30
  constructor(options: DateFieldOptions<Validate>) {
31
+ const { minimumDate, maximumDate, ...rest } = options
32
+
33
+ const schema = z.preprocess((val: unknown) => {
34
+ const normalized = normalizeToDate(val)
35
+
36
+ if (!normalized || isNaN(normalized.getTime())) return undefined
37
+ return normalized
38
+ }, z.date()
39
+ .refine(d => !minimumDate || d >= minimumDate, {
40
+ message: `Date must be on or after ${minimumDate?.toDateString()}`,
41
+ })
42
+ .refine(d => !maximumDate || d <= maximumDate, {
43
+ message: `Date must be on or before ${maximumDate?.toDateString()}`,
44
+ })
45
+ )
46
+
17
47
  super({
18
- validate: zodValidator(z.date()) as unknown as Validate,
19
- ...options
48
+ validate: zodValidator(schema) as unknown as Validate,
49
+ ...options,
20
50
  })
21
51
  }
22
52
  }