@codeleap/form 5.4.4 → 5.4.5

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.4",
3
+ "version": "5.4.5",
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.4",
13
- "@codeleap/types": "5.4.4",
14
- "@codeleap/store": "5.4.4",
12
+ "@codeleap/config": "5.4.5",
13
+ "@codeleap/types": "5.4.5",
14
+ "@codeleap/store": "5.4.5",
15
15
  "zod": "3.23.8",
16
16
  "ts-node-dev": "1.1.8"
17
17
  },
@@ -20,12 +20,11 @@
20
20
  "playground": "bun src/test.ts"
21
21
  },
22
22
  "peerDependencies": {
23
- "@codeleap/types": "5.4.4",
24
- "@codeleap/store": "5.4.4",
25
- "@codeleap/logger": "5.4.4",
23
+ "@codeleap/types": "5.4.5",
24
+ "@codeleap/store": "5.4.5",
25
+ "@codeleap/logger": "5.4.5",
26
26
  "zod": "*",
27
27
  "react": "18.2.0",
28
- "typescript": "5.5.2",
29
- "yup": "1.6.1"
28
+ "typescript": "5.5.2"
30
29
  }
31
30
  }
package/package.json.bak CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/form",
3
- "version": "5.4.4",
3
+ "version": "5.4.5",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -25,7 +25,6 @@
25
25
  "@codeleap/logger": "workspace:*",
26
26
  "zod": "*",
27
27
  "react": "18.2.0",
28
- "typescript": "5.5.2",
29
- "yup": "1.6.1"
28
+ "typescript": "5.5.2"
30
29
  }
31
30
  }
@@ -1 +1,2 @@
1
- export { useField } from './useField'
1
+ export { useField } from './useField'
2
+ export { useValidate } from './useValidate'
@@ -0,0 +1,50 @@
1
+ import { useCallback, useRef, useState } from 'react'
2
+ import { Validator } from '../types'
3
+ import { ValidationError } from '../lib/Field'
4
+
5
+ export const useValidate = <T, V extends Validator<T, any, any>>(value: T, providedValidate: V) => {
6
+ const [hasBlurred, setHasBlurred] = useState(false)
7
+
8
+ const isUnset = typeof value === 'undefined'
9
+
10
+ const startedUnset = useRef(isUnset).current
11
+
12
+ const validate = useCallback((value: T) => {
13
+ try {
14
+ const result = providedValidate?.(value, {})
15
+
16
+ return result
17
+ } catch (e) {
18
+ if (e instanceof ValidationError) {
19
+ return {
20
+ isValid: false,
21
+ error: e.data
22
+ }
23
+ }
24
+
25
+ throw e
26
+ }
27
+ }, [])
28
+
29
+ const onInputBlurred = useCallback(() => {
30
+ setHasBlurred(true)
31
+ }, [])
32
+
33
+ const validation = validate(value)
34
+
35
+ const isValid = validation?.isValid ?? true
36
+
37
+ const isInvalid = !isValid
38
+
39
+ const message = validation?.readableError ?? validation?.error?.[0]?.message
40
+
41
+ const errorDisplayRequiresBlur = startedUnset
42
+
43
+ const showError = isInvalid && (errorDisplayRequiresBlur ? hasBlurred : true)
44
+
45
+ return {
46
+ onInputBlurred,
47
+ showError,
48
+ message,
49
+ }
50
+ }
package/src/lib/Field.ts CHANGED
@@ -9,7 +9,7 @@ import { useFieldBinding } from './useFieldBinding';
9
9
  import { createRef, useRef, useState } from 'react';
10
10
  import { logger } from '@codeleap/logger';
11
11
 
12
- class ValidationError extends Error {
12
+ export class ValidationError extends Error {
13
13
  data: any
14
14
 
15
15
  constructor(data: any) {