@bison-lab/ui 1.7.0 → 1.9.0
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/dist/index.d.mts +634 -7
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1217 -147
- package/dist/index.mjs.map +1 -1
- package/dist/validators.d.mts +74 -0
- package/dist/validators.d.mts.map +1 -0
- package/dist/validators.mjs +62 -0
- package/dist/validators.mjs.map +1 -0
- package/package.json +6 -2
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/form/validators.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Shared field schemas for `<Form schema={...}>`.
|
|
6
|
+
*
|
|
7
|
+
* Published from `@bison-lab/ui/validators`, NOT the package root. Every output
|
|
8
|
+
* of the main entry carries a `"use client"` banner, and these are meant to run
|
|
9
|
+
* in Server Actions and route handlers as well as the browser — importing them
|
|
10
|
+
* from the root fails at runtime with "Attempted to call … from the server but
|
|
11
|
+
* … is on the client". Keep this file free of React and of anything holding a
|
|
12
|
+
* `'use client'` directive.
|
|
13
|
+
*
|
|
14
|
+
* These exist so the rules that go with the library's own controls ship with
|
|
15
|
+
* the library. The phone case is the reason they had to: `PhoneInput` submits
|
|
16
|
+
* its FORMATTED display value — `(555) 123-4567`, not `5551234567` — because
|
|
17
|
+
* the visible input is the one carrying the `name`. Every consumer writing
|
|
18
|
+
* their own `z.string().length(10)` against that field gets a schema that can
|
|
19
|
+
* never pass, and the failure looks like a validation bug rather than a
|
|
20
|
+
* formatting mismatch. {@link usPhoneField} counts digits instead.
|
|
21
|
+
*/
|
|
22
|
+
interface TextFieldOptions {
|
|
23
|
+
/** Message shown when the field is empty. */
|
|
24
|
+
requiredMessage?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A trimmed, non-empty string.
|
|
28
|
+
*
|
|
29
|
+
* Trimming happens before the emptiness check, so a field holding only spaces
|
|
30
|
+
* fails rather than passing as "filled".
|
|
31
|
+
*/
|
|
32
|
+
declare function requiredText({
|
|
33
|
+
requiredMessage
|
|
34
|
+
}?: TextFieldOptions): z.ZodString;
|
|
35
|
+
interface EmailFieldOptions extends TextFieldOptions {
|
|
36
|
+
/** Message shown when the value is present but not an email address. */
|
|
37
|
+
message?: string;
|
|
38
|
+
/** Allow an empty value. @default false */
|
|
39
|
+
optional?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** An email address. Trimmed, and empty-tolerant when `optional`. */
|
|
42
|
+
declare function emailField({
|
|
43
|
+
requiredMessage,
|
|
44
|
+
message,
|
|
45
|
+
optional
|
|
46
|
+
}?: EmailFieldOptions): z.ZodString | z.ZodEffects<z.ZodString, string, string>;
|
|
47
|
+
interface UsPhoneFieldOptions extends TextFieldOptions {
|
|
48
|
+
/** Message shown when the value is present but not ten digits. */
|
|
49
|
+
message?: string;
|
|
50
|
+
/** Require a value. @default false — phone is usually a secondary channel. */
|
|
51
|
+
required?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A US (NANP) phone number, counted in digits rather than matched by shape.
|
|
55
|
+
*
|
|
56
|
+
* Accepts anything `PhoneInput` can produce or a human can paste:
|
|
57
|
+
* `(555) 123-4567`, `555-123-4567`, `5551234567`, `+1 555 123 4567`. What it
|
|
58
|
+
* checks is that exactly ten digits are present, which is the only part that
|
|
59
|
+
* carries meaning — `PhoneInput` already owns the presentation.
|
|
60
|
+
*/
|
|
61
|
+
declare function usPhoneField({
|
|
62
|
+
requiredMessage,
|
|
63
|
+
message,
|
|
64
|
+
required
|
|
65
|
+
}?: UsPhoneFieldOptions): z.ZodEffects<z.ZodString, string, string>;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/lib/us-phone.d.ts
|
|
68
|
+
/** Strips non-digits and caps at 10 (US NANP). */
|
|
69
|
+
declare function parseUsPhoneDigits(value: string): string;
|
|
70
|
+
/** Formats a phone value (digits, E.164, or any mixed string) as `(XXX) XXX-XXXX`. */
|
|
71
|
+
declare function formatUsPhone(value: string): string;
|
|
72
|
+
//#endregion
|
|
73
|
+
export { type EmailFieldOptions, type TextFieldOptions, type UsPhoneFieldOptions, emailField, formatUsPhone, parseUsPhoneDigits, requiredText, usPhoneField };
|
|
74
|
+
//# sourceMappingURL=validators.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.d.mts","names":[],"sources":["../src/form/validators.ts","../src/lib/us-phone.ts"],"mappings":";;;;;AAsBA;;;;;AAWA;;;;;;;;;;;UAXiB,gBAAA;EAaO;EAXtB,eAAA;AAAA;AAeF;;;;;;AAAA,iBANgB,YAAA,CAAA;EACd;AAAA,IACC,gBAAA,GAAqB,CAAA,CAAA,SAAA;AAAA,UAIP,iBAAA,SAA0B,gBAAA;EAIjC;EAFR,OAAA;EAMwB;EAJxB,QAAA;AAAA;;iBAIc,UAAA,CAAA;EACd,eAAA;EACA,OAAA;EACA;AAAA,IACC,iBAAA,GAAsB,CAAA,CAAA,SAAA,GAAA,CAAA,CAAA,UAAA,CAAA,CAAA,CAAA,SAAA;AAAA,UAYR,mBAAA,SAA4B,gBAAA;EAZpB;EAcvB,OAAA;EAduB;EAgBvB,QAAA;AAAA;;;;;;;;;iBAWc,YAAA,CAAA;EACd,eAAA;EACA,OAAA;EACA;AAAA,IACC,mBAAA,GAAwB,CAAA,CAAA,UAAA,CAAA,CAAA,CAAA,SAAA;;;;iBC/EX,kBAAA,CAAmB,KAAA;;iBAKnB,aAAA,CAAc,KAAA"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
//#region src/lib/us-phone.ts
|
|
3
|
+
const PHONE_MAX_DIGITS = 10;
|
|
4
|
+
/** Strips non-digits and caps at 10 (US NANP). */
|
|
5
|
+
function parseUsPhoneDigits(value) {
|
|
6
|
+
return value.replace(/\D/g, "").slice(0, PHONE_MAX_DIGITS);
|
|
7
|
+
}
|
|
8
|
+
/** Formats a phone value (digits, E.164, or any mixed string) as `(XXX) XXX-XXXX`. */
|
|
9
|
+
function formatUsPhone(value) {
|
|
10
|
+
const d = parseUsPhoneDigits(value);
|
|
11
|
+
if (d.length <= 3) return d;
|
|
12
|
+
if (d.length <= 6) return `(${d.slice(0, 3)}) ${d.slice(3)}`;
|
|
13
|
+
return `(${d.slice(0, 3)}) ${d.slice(3, 6)}-${d.slice(6)}`;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Deliberately NOT a `'use client'` module, and deliberately not living in
|
|
17
|
+
* `phone-input.tsx` any more.
|
|
18
|
+
*
|
|
19
|
+
* `validators.ts` needs `parseUsPhoneDigits`, and it is consumed from Server
|
|
20
|
+
* Actions. Importing it from the component file dragged the whole client
|
|
21
|
+
* component in behind it, and every output of the main entry carries a
|
|
22
|
+
* `"use client"` banner — so calling the validator on the server failed with
|
|
23
|
+
* "Attempted to call … from the server but … is on the client".
|
|
24
|
+
*
|
|
25
|
+
* Keep this file free of React, and free of imports from anything holding a
|
|
26
|
+
* `'use client'` directive.
|
|
27
|
+
*/
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/form/validators.ts
|
|
30
|
+
/**
|
|
31
|
+
* A trimmed, non-empty string.
|
|
32
|
+
*
|
|
33
|
+
* Trimming happens before the emptiness check, so a field holding only spaces
|
|
34
|
+
* fails rather than passing as "filled".
|
|
35
|
+
*/
|
|
36
|
+
function requiredText({ requiredMessage = "This field is required." } = {}) {
|
|
37
|
+
return z.string().trim().min(1, { message: requiredMessage });
|
|
38
|
+
}
|
|
39
|
+
/** An email address. Trimmed, and empty-tolerant when `optional`. */
|
|
40
|
+
function emailField({ requiredMessage = "Email address is required.", message = "Enter a valid email address.", optional = false } = {}) {
|
|
41
|
+
const base = z.string().trim();
|
|
42
|
+
if (optional) return base.refine((value) => value === "" || z.string().email().safeParse(value).success, { message });
|
|
43
|
+
return base.min(1, { message: requiredMessage }).email({ message });
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A US (NANP) phone number, counted in digits rather than matched by shape.
|
|
47
|
+
*
|
|
48
|
+
* Accepts anything `PhoneInput` can produce or a human can paste:
|
|
49
|
+
* `(555) 123-4567`, `555-123-4567`, `5551234567`, `+1 555 123 4567`. What it
|
|
50
|
+
* checks is that exactly ten digits are present, which is the only part that
|
|
51
|
+
* carries meaning — `PhoneInput` already owns the presentation.
|
|
52
|
+
*/
|
|
53
|
+
function usPhoneField({ requiredMessage = "Phone number is required.", message = "Enter a 10-digit US phone number.", required = false } = {}) {
|
|
54
|
+
const base = z.string().trim();
|
|
55
|
+
const hasTenDigits = (value) => parseUsPhoneDigits(value).length === 10;
|
|
56
|
+
if (required) return base.min(1, { message: requiredMessage }).refine(hasTenDigits, { message });
|
|
57
|
+
return base.refine((value) => value === "" || hasTenDigits(value), { message });
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { emailField, formatUsPhone, parseUsPhoneDigits, requiredText, usPhoneField };
|
|
61
|
+
|
|
62
|
+
//# sourceMappingURL=validators.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.mjs","names":[],"sources":["../src/lib/us-phone.ts","../src/form/validators.ts"],"sourcesContent":["const PHONE_MAX_DIGITS = 10;\n\n/** Strips non-digits and caps at 10 (US NANP). */\nexport function parseUsPhoneDigits(value: string): string {\n return value.replace(/\\D/g, '').slice(0, PHONE_MAX_DIGITS);\n}\n\n/** Formats a phone value (digits, E.164, or any mixed string) as `(XXX) XXX-XXXX`. */\nexport function formatUsPhone(value: string): string {\n const d = parseUsPhoneDigits(value);\n if (d.length <= 3) return d;\n if (d.length <= 6) return `(${d.slice(0, 3)}) ${d.slice(3)}`;\n return `(${d.slice(0, 3)}) ${d.slice(3, 6)}-${d.slice(6)}`;\n}\n\n/**\n * Deliberately NOT a `'use client'` module, and deliberately not living in\n * `phone-input.tsx` any more.\n *\n * `validators.ts` needs `parseUsPhoneDigits`, and it is consumed from Server\n * Actions. Importing it from the component file dragged the whole client\n * component in behind it, and every output of the main entry carries a\n * `\"use client\"` banner — so calling the validator on the server failed with\n * \"Attempted to call … from the server but … is on the client\".\n *\n * Keep this file free of React, and free of imports from anything holding a\n * `'use client'` directive.\n */\n","import { z } from 'zod';\nimport { parseUsPhoneDigits } from '../lib/us-phone';\n\n/**\n * Shared field schemas for `<Form schema={...}>`.\n *\n * Published from `@bison-lab/ui/validators`, NOT the package root. Every output\n * of the main entry carries a `\"use client\"` banner, and these are meant to run\n * in Server Actions and route handlers as well as the browser — importing them\n * from the root fails at runtime with \"Attempted to call … from the server but\n * … is on the client\". Keep this file free of React and of anything holding a\n * `'use client'` directive.\n *\n * These exist so the rules that go with the library's own controls ship with\n * the library. The phone case is the reason they had to: `PhoneInput` submits\n * its FORMATTED display value — `(555) 123-4567`, not `5551234567` — because\n * the visible input is the one carrying the `name`. Every consumer writing\n * their own `z.string().length(10)` against that field gets a schema that can\n * never pass, and the failure looks like a validation bug rather than a\n * formatting mismatch. {@link usPhoneField} counts digits instead.\n */\n\nexport interface TextFieldOptions {\n /** Message shown when the field is empty. */\n requiredMessage?: string;\n}\n\n/**\n * A trimmed, non-empty string.\n *\n * Trimming happens before the emptiness check, so a field holding only spaces\n * fails rather than passing as \"filled\".\n */\nexport function requiredText({\n requiredMessage = 'This field is required.',\n}: TextFieldOptions = {}) {\n return z.string().trim().min(1, { message: requiredMessage });\n}\n\nexport interface EmailFieldOptions extends TextFieldOptions {\n /** Message shown when the value is present but not an email address. */\n message?: string;\n /** Allow an empty value. @default false */\n optional?: boolean;\n}\n\n/** An email address. Trimmed, and empty-tolerant when `optional`. */\nexport function emailField({\n requiredMessage = 'Email address is required.',\n message = 'Enter a valid email address.',\n optional = false,\n}: EmailFieldOptions = {}) {\n const base = z.string().trim();\n\n if (optional) {\n return base.refine((value) => value === '' || z.string().email().safeParse(value).success, {\n message,\n });\n }\n\n return base.min(1, { message: requiredMessage }).email({ message });\n}\n\nexport interface UsPhoneFieldOptions extends TextFieldOptions {\n /** Message shown when the value is present but not ten digits. */\n message?: string;\n /** Require a value. @default false — phone is usually a secondary channel. */\n required?: boolean;\n}\n\n/**\n * A US (NANP) phone number, counted in digits rather than matched by shape.\n *\n * Accepts anything `PhoneInput` can produce or a human can paste:\n * `(555) 123-4567`, `555-123-4567`, `5551234567`, `+1 555 123 4567`. What it\n * checks is that exactly ten digits are present, which is the only part that\n * carries meaning — `PhoneInput` already owns the presentation.\n */\nexport function usPhoneField({\n requiredMessage = 'Phone number is required.',\n message = 'Enter a 10-digit US phone number.',\n required = false,\n}: UsPhoneFieldOptions = {}) {\n const base = z.string().trim();\n const hasTenDigits = (value: string) => parseUsPhoneDigits(value).length === 10;\n\n if (required) {\n return base.min(1, { message: requiredMessage }).refine(hasTenDigits, { message });\n }\n\n return base.refine((value) => value === '' || hasTenDigits(value), { message });\n}\n"],"mappings":";;AAAA,MAAM,mBAAmB;;AAGzB,SAAgB,mBAAmB,OAAuB;AACxD,QAAO,MAAM,QAAQ,OAAO,GAAG,CAAC,MAAM,GAAG,iBAAiB;;;AAI5D,SAAgB,cAAc,OAAuB;CACnD,MAAM,IAAI,mBAAmB,MAAM;AACnC,KAAI,EAAE,UAAU,EAAG,QAAO;AAC1B,KAAI,EAAE,UAAU,EAAG,QAAO,IAAI,EAAE,MAAM,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE;AAC1D,QAAO,IAAI,EAAE,MAAM,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE;;;;;;;;;;;;;;;;;;;;;;;ACqB1D,SAAgB,aAAa,EAC3B,kBAAkB,8BACE,EAAE,EAAE;AACxB,QAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,GAAG,EAAE,SAAS,iBAAiB,CAAC;;;AAW/D,SAAgB,WAAW,EACzB,kBAAkB,8BAClB,UAAU,gCACV,WAAW,UACU,EAAE,EAAE;CACzB,MAAM,OAAO,EAAE,QAAQ,CAAC,MAAM;AAE9B,KAAI,SACF,QAAO,KAAK,QAAQ,UAAU,UAAU,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,UAAU,MAAM,CAAC,SAAS,EACzF,SACD,CAAC;AAGJ,QAAO,KAAK,IAAI,GAAG,EAAE,SAAS,iBAAiB,CAAC,CAAC,MAAM,EAAE,SAAS,CAAC;;;;;;;;;;AAkBrE,SAAgB,aAAa,EAC3B,kBAAkB,6BAClB,UAAU,qCACV,WAAW,UACY,EAAE,EAAE;CAC3B,MAAM,OAAO,EAAE,QAAQ,CAAC,MAAM;CAC9B,MAAM,gBAAgB,UAAkB,mBAAmB,MAAM,CAAC,WAAW;AAE7E,KAAI,SACF,QAAO,KAAK,IAAI,GAAG,EAAE,SAAS,iBAAiB,CAAC,CAAC,OAAO,cAAc,EAAE,SAAS,CAAC;AAGpF,QAAO,KAAK,QAAQ,UAAU,UAAU,MAAM,aAAa,MAAM,EAAE,EAAE,SAAS,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bison-lab/ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "React components, form system, and blocks for Bison Lab component library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -13,6 +13,10 @@
|
|
|
13
13
|
"./charts": {
|
|
14
14
|
"import": "./dist/charts.mjs",
|
|
15
15
|
"types": "./dist/charts.d.mts"
|
|
16
|
+
},
|
|
17
|
+
"./validators": {
|
|
18
|
+
"import": "./dist/validators.mjs",
|
|
19
|
+
"types": "./dist/validators.d.mts"
|
|
16
20
|
}
|
|
17
21
|
},
|
|
18
22
|
"files": [
|
|
@@ -40,7 +44,7 @@
|
|
|
40
44
|
"sonner": "^2.0.7",
|
|
41
45
|
"tailwind-merge": "^3.5.0",
|
|
42
46
|
"zod": "^3.24.0",
|
|
43
|
-
"@bison-lab/tokens": "1.
|
|
47
|
+
"@bison-lab/tokens": "1.9.0"
|
|
44
48
|
},
|
|
45
49
|
"peerDependencies": {
|
|
46
50
|
"react": "^18.0.0 || ^19.0.0",
|