Skip to content

Commit c198547

Browse files
authored
[0.5.x] Re-work on-change validation (#105)
* Update types * Re-work validating on change * Improve watch command * Remove comment
1 parent 07fe9d9 commit c198547

File tree

8 files changed

+14
-20
lines changed

8 files changed

+14
-20
lines changed

packages/alpine/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"/dist"
2121
],
2222
"scripts": {
23-
"watch": "rm -rf dist && tsc --watch",
23+
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
2424
"build": "rm -rf dist && tsc",
2525
"typeCheck": "tsc --noEmit",
2626
"prepublishOnly": "npm run build",

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"/dist"
2121
],
2222
"scripts": {
23-
"watch": "rm -rf dist && tsc --watch",
23+
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
2424
"build": "rm -rf dist && tsc",
2525
"typeCheck": "tsc --noEmit",
2626
"prepublishOnly": "npm run build",

packages/core/src/validator.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
169169
/**
170170
* Create a debounced validation callback.
171171
*/
172-
const createValidator = () => debounce((instanceConfig: Config) => {
172+
const createValidator = () => debounce((instanceConfig: ValidationConfig) => {
173173
callback({
174174
get: (url, data = {}, globalConfig = {}) => client.get(url, parseData(data), resolveConfig(globalConfig, instanceConfig, data)),
175175
post: (url, data = {}, globalConfig = {}) => client.post(url, parseData(data), resolveConfig(globalConfig, instanceConfig, data)),
@@ -254,11 +254,7 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
254254
: response
255255
},
256256
onBefore: () => {
257-
const beforeValidationHandler = config.onBeforeValidation ?? ((newRequest, oldRequest) => {
258-
return newRequest.touched.length > 0 && ! isEqual(newRequest, oldRequest)
259-
})
260-
261-
if (beforeValidationHandler({ data, touched }, { data: oldData, touched: oldTouched }) === false) {
257+
if (config.onBeforeValidation && config.onBeforeValidation({ data, touched }, { data: oldData, touched: oldTouched }) === false) {
262258
return false
263259
}
264260

@@ -296,7 +292,7 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
296292
/**
297293
* Validate the given input.
298294
*/
299-
const validate = (name?: string | NamedInputEvent, value?: unknown, config?: Config): void => {
295+
const validate = (name?: string | NamedInputEvent, value?: unknown, config?: ValidationConfig): void => {
300296
if (typeof name === 'undefined') {
301297
validator(config ?? {})
302298

@@ -313,9 +309,9 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
313309

314310
if (get(oldData, name) !== value) {
315311
setTouched([name, ...touched]).forEach((listener) => listener())
316-
}
317312

318-
validator(config ?? {})
313+
validator(config ?? {})
314+
}
319315
}
320316

321317
/**

packages/core/tests/validator.test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ it('does not revalidate data when data is unchanged', async () => {
105105
expect(requests).toBe(0)
106106

107107
data = { first: true }
108-
validator.validate('name', true)
108+
validator.validate('first', true)
109109
expect(requests).toBe(1)
110110
await vi.advanceTimersByTimeAsync(1500)
111111

112112
data = { first: true }
113-
validator.validate('name', true)
113+
validator.validate('first', true)
114114
expect(requests).toBe(1)
115115
await vi.advanceTimersByTimeAsync(1500)
116116

117117
data = { second: true }
118-
validator.validate('name', true)
118+
validator.validate('second', true)
119119
expect(requests).toBe(2)
120120
await vi.advanceTimersByTimeAsync(1500)
121121
})
@@ -245,8 +245,6 @@ it('does not validate if the field has not been changed', async () => {
245245
validator.validate('name', 'Tim')
246246

247247
expect(requestMade).toBe(false)
248-
249-
await assertPendingValidateDebounceAndClear()
250248
})
251249

252250
it('filters out files', async () => {

packages/react-inertia/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"/dist"
2222
],
2323
"scripts": {
24-
"watch": "rm -rf dist && tsc --watch",
24+
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
2525
"build": "rm -rf dist && tsc",
2626
"typeCheck": "tsc --noEmit",
2727
"prepublishOnly": "npm run build",

packages/react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"/dist"
2121
],
2222
"scripts": {
23-
"watch": "rm -rf dist && tsc --watch",
23+
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
2424
"build": "rm -rf dist && tsc",
2525
"typeCheck": "tsc --noEmit",
2626
"prepublishOnly": "npm run build",

packages/vue-inertia/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"/dist"
2222
],
2323
"scripts": {
24-
"watch": "rm -rf dist && tsc --watch",
24+
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
2525
"build": "rm -rf dist && tsc",
2626
"typeCheck": "tsc --noEmit",
2727
"prepublishOnly": "npm run build",

packages/vue/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"/dist"
2121
],
2222
"scripts": {
23-
"watch": "rm -rf dist && tsc --watch",
23+
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
2424
"build": "rm -rf dist && tsc",
2525
"typeCheck": "tsc --noEmit",
2626
"prepublishOnly": "npm run build",

0 commit comments

Comments
 (0)