Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.5.x] Re-work on-change validation #105

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/alpine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"/dist"
],
"scripts": {
"watch": "rm -rf dist && tsc --watch",
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
"build": "rm -rf dist && tsc",
"typeCheck": "tsc --noEmit",
"prepublishOnly": "npm run build",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"/dist"
],
"scripts": {
"watch": "rm -rf dist && tsc --watch",
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
"build": "rm -rf dist && tsc",
"typeCheck": "tsc --noEmit",
"prepublishOnly": "npm run build",
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
/**
* Create a debounced validation callback.
*/
const createValidator = () => debounce((instanceConfig: Config) => {
const createValidator = () => debounce((instanceConfig: ValidationConfig) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small type improvement.

callback({
get: (url, data = {}, globalConfig = {}) => client.get(url, parseData(data), resolveConfig(globalConfig, instanceConfig, data)),
post: (url, data = {}, globalConfig = {}) => client.post(url, parseData(data), resolveConfig(globalConfig, instanceConfig, data)),
Expand Down Expand Up @@ -254,11 +254,7 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
: response
},
onBefore: () => {
const beforeValidationHandler = config.onBeforeValidation ?? ((newRequest, oldRequest) => {
return newRequest.touched.length > 0 && ! isEqual(newRequest, oldRequest)
})

if (beforeValidationHandler({ data, touched }, { data: oldData, touched: oldTouched }) === false) {
if (config.onBeforeValidation && config.onBeforeValidation({ data, touched }, { data: oldData, touched: oldTouched }) === false) {
return false
}

Expand Down Expand Up @@ -296,7 +292,7 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
/**
* Validate the given input.
*/
const validate = (name?: string | NamedInputEvent, value?: unknown, config?: Config): void => {
const validate = (name?: string | NamedInputEvent, value?: unknown, config?: ValidationConfig): void => {
Comment on lines -299 to +295
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small type improvement.

if (typeof name === 'undefined') {
validator(config ?? {})

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

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

validator(config ?? {})
validator(config ?? {})
}
}

/**
Expand Down
8 changes: 3 additions & 5 deletions packages/core/tests/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ it('does not revalidate data when data is unchanged', async () => {
expect(requests).toBe(0)

data = { first: true }
validator.validate('name', true)
validator.validate('first', true)
expect(requests).toBe(1)
await vi.advanceTimersByTimeAsync(1500)

data = { first: true }
validator.validate('name', true)
validator.validate('first', true)
expect(requests).toBe(1)
await vi.advanceTimersByTimeAsync(1500)

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

expect(requestMade).toBe(false)

await assertPendingValidateDebounceAndClear()
})

it('filters out files', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-inertia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"/dist"
],
"scripts": {
"watch": "rm -rf dist && tsc --watch",
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
"build": "rm -rf dist && tsc",
"typeCheck": "tsc --noEmit",
"prepublishOnly": "npm run build",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"/dist"
],
"scripts": {
"watch": "rm -rf dist && tsc --watch",
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
"build": "rm -rf dist && tsc",
"typeCheck": "tsc --noEmit",
"prepublishOnly": "npm run build",
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-inertia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"/dist"
],
"scripts": {
"watch": "rm -rf dist && tsc --watch",
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
"build": "rm -rf dist && tsc",
"typeCheck": "tsc --noEmit",
"prepublishOnly": "npm run build",
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"/dist"
],
"scripts": {
"watch": "rm -rf dist && tsc --watch",
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput",
"build": "rm -rf dist && tsc",
"typeCheck": "tsc --noEmit",
"prepublishOnly": "npm run build",
Expand Down
Loading