@qaflo/forms-client 1.1.0 → 1.2.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/README.md CHANGED
@@ -106,6 +106,9 @@ const { formProps, fieldProps, errors, composed } = forms.useContactForm({
106
106
  email: { required: true, email: true, max: 254 },
107
107
  phone: { required: true, min: 5, max: 32 }, // optional to the service
108
108
  message: { required: true, min: 10, max: 5000 },
109
+ // A shape the service has no opinion on. Checked only when the field has
110
+ // something in it, so an optional field with a pattern stays optional.
111
+ licence: { pattern: /^[0-9]{6}$/ },
109
112
  },
110
113
 
111
114
  // The rules are the service's. The voice is yours.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qaflo/forms-client",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Client half of the shared qaflo contact-form service: transport, Turnstile lifecycle and visitor-facing status messages.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -22,7 +22,8 @@
22
22
  "./messages": {
23
23
  "types": "./messages.d.ts",
24
24
  "default": "./messages.js"
25
- }
25
+ },
26
+ "./package.json": "./package.json"
26
27
  },
27
28
  "files": [
28
29
  "*.js",
package/validate.d.ts CHANGED
@@ -3,6 +3,8 @@ export interface FieldRule {
3
3
  min?: number
4
4
  max?: number
5
5
  email?: boolean
6
+ /** A site-specific shape. Checked only when the field is non-empty. */
7
+ pattern?: RegExp
6
8
  /** Used in the default messages; defaults to the field's own name. */
7
9
  label?: string
8
10
  }
@@ -14,6 +16,7 @@ export type FieldMessage =
14
16
  export interface FieldMessages {
15
17
  required?: FieldMessage
16
18
  email?: FieldMessage
19
+ pattern?: FieldMessage
17
20
  min?: FieldMessage
18
21
  max?: FieldMessage
19
22
  }
package/validate.js CHANGED
@@ -35,6 +35,7 @@ const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
35
35
  const DEFAULTS = {
36
36
  required: (label) => `Please enter your ${label}.`,
37
37
  email: () => 'That does not look like a valid email address.',
38
+ pattern: (label) => `That ${label} does not look right.`,
38
39
  min: (label, n) => `That looks too short — please enter at least ${n} characters.`,
39
40
  max: (label, n) => `That is too long — ${n} characters at most.`,
40
41
  }
@@ -81,6 +82,11 @@ export function validateFields(values, fields = DEFAULT_FIELDS, messages) {
81
82
  }
82
83
  if (rule.email && !EMAIL_RE.test(value)) {
83
84
  errors[field] = message(messages, field, 'email', label, 0, value)
85
+ } else if (rule.pattern && !rule.pattern.test(value)) {
86
+ // A site-specific shape the service cannot know about — a UAE mobile
87
+ // number, a trade licence, a postcode. Checked only when the field has
88
+ // something in it, so an optional field with a pattern stays optional.
89
+ errors[field] = message(messages, field, 'pattern', label, 0, value)
84
90
  } else if (rule.min && value.length < rule.min) {
85
91
  errors[field] = message(messages, field, 'min', label, rule.min, value)
86
92
  } else if (rule.max && value.length > rule.max) {