Skip to content

Commit

Permalink
style: update code lint
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Feb 20, 2024
1 parent b2027cd commit 2f54207
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 113 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ const { form, status, reset, dirtyFields } = useForm({
<input v-model="form.username" type="text">
<input v-model="form.password" type="text">
</label>
<button type="submit">Submit</button>
<button type="submit">
Submit
</button>
</form>
</template>
```
Expand Down Expand Up @@ -178,9 +180,8 @@ Use `rule` to define the validation rules for form fields. The verification proc
```ts
// formRules.ts
function isRequired(value) {
if (value && value.trim()) {
if (value && value.trim())
return true
}

return t('required') // i18n support
}
Expand Down Expand Up @@ -352,9 +353,8 @@ const { form, rule } = useForm({

const text = 'abcd'
const isValid = rule.userName.validate(text) // false
if (isValid) {
if (isValid)
form.userName = text
}
```

You can also get access to the error message by indicating `fullResult: true` in the second options argument, in which case an object containing the message will be returned.
Expand Down Expand Up @@ -474,10 +474,12 @@ Some suggestions:
v-model="form.age"
type="text"
:class="status.age.isError && '!border-red'"
/>
>
<p>{{ status.age.message }}</p>
</label>
<button type="submit">Submit</button>
<button type="submit">
Submit
</button>
</form>
</template>
```
10 changes: 5 additions & 5 deletions README.zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ const { form, status, reset, dirtyFields } = useForm({
<input v-model="form.username" type="text">
<input v-model="form.password" type="text">
</label>
<button type="submit">提交</button>
<button type="submit">
提交
</button>
</form>
</template>
```
Expand Down Expand Up @@ -176,9 +178,8 @@ dirtyFields.value /* value: {} */
```ts
// formRules.ts
export function isRequired(value) {
if (value && value.trim()) {
if (value && value.trim())
return true
}

return t('required') // i18n 支持
}
Expand Down Expand Up @@ -350,9 +351,8 @@ const { form, rule } = useForm({

const text = 'abcd'
const isValid = rule.userName.validate(text) // false
if (isValid) {
if (isValid)
form.userName = text
}
```

您还可以获取到该字段的错误信息,方法是在第二个选项参数里传入 `fullResult: true`。此时该方法会返回一个带有错误信息的对象。
Expand Down
1 change: 1 addition & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare module '*.vue' {
import type { ComponentOptions } from 'vue'

const componentOptions: ComponentOptions
export default componentOptions
}
18 changes: 9 additions & 9 deletions example/netlify.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[build.environment]
NPM_FLAGS = "--version"
NODE_VERSION = "16"

[build]
publish = "dist"
command = "npx pnpm i --store=node_modules/.pnpm-store && npx pnpm run build"
publish = "dist"
command = "npx pnpm i --store=node_modules/.pnpm-store && npx pnpm run build"

[build.environment]
NPM_FLAGS = "--version"
NODE_VERSION = "16"

[[redirects]]
from = "/*"
to = "/index.html"
status = 200
from = "/*"
to = "/index.html"
status = 200
3 changes: 2 additions & 1 deletion example/shims.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>

const component: DefineComponent
export default component
}
2 changes: 1 addition & 1 deletion example/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

describe('Hi', () => {
describe('hi', () => {
it('should works', () => {
expect(1 + 1).toEqual(2)
})
Expand Down
2 changes: 1 addition & 1 deletion example/test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import Counter from '../src/components/Counter.vue'

describe('Counter.vue', () => {
describe('counter.vue', () => {
it('should render', () => {
const wrapper = mount(Counter, { props: { initial: 10 } })
expect(wrapper.text()).toContain('10')
Expand Down
26 changes: 13 additions & 13 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"compilerOptions": {
"baseUrl": ".",
"module": "ESNext",
"target": "es2016",
"lib": ["DOM", "ESNext"],
"strict": true,
"jsx": "preserve",
"esModuleInterop": true,
"skipLibCheck": true,
"lib": ["DOM", "ESNext"],
"baseUrl": ".",
"module": "ESNext",
"moduleResolution": "node",
"paths": {
"~/*": ["src/*"],
"root/*": ["./../*"]
},
"resolveJsonModule": true,
"noUnusedLocals": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"types": [
"vite/client",
"vite-plugin-pages/client"
],
"paths": {
"~/*": ["src/*"],
"root/*": ["./../*"]
}
"strict": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
},
"exclude": ["dist", "node_modules"]
}
4 changes: 2 additions & 2 deletions example/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="vitest" />

import path from 'path'
import { fileURLToPath } from 'url'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import Vue from '@vitejs/plugin-vue'
import Unocss from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
Expand Down
2 changes: 1 addition & 1 deletion packages/.test/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Component, InjectionKey, Ref } from 'vue'
import { createApp, defineComponent, h, provide, ref } from 'vue'

type InstanceType<V> = V extends { new (...arg: any[]): infer X } ? X : never
type VM<V> = InstanceType<V> & { unmount(): void }
type VM<V> = InstanceType<V> & { unmount: () => void }

export function mount<V extends Component>(Comp: V) {
const el = document.createElement('div')
Expand Down
4 changes: 2 additions & 2 deletions packages/resolvers/yup.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, test } from 'vitest'
import { expect, it } from 'vitest'
import { nextTick, ref } from 'vue'
import * as yup from 'yup'
import { useForm } from '../../src'
import { useSetup } from '../.test'
import { yupFieldRule } from './yup'

test('yupFieldRule', async () => {
it('yupFieldRule', async () => {
const wr = useSetup(() => {
const local = ref('en')
/** mock i18n `t` function */
Expand Down
7 changes: 2 additions & 5 deletions packages/resolvers/yup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Schema, ValidationError, AnyObject, ValidateOptions } from 'yup'
import type { AnyObject, Schema, ValidateOptions, ValidationError } from 'yup'

export interface ResolverOptions {
model?: 'validateSync' | 'validate'
}

/** yup field rule resolver */
export const yupFieldRule = <SchemaT extends Schema, TContext = {}>(
fieldSchema: SchemaT,
schemaOptions: ValidateOptions<TContext> = {},
) => {
export function yupFieldRule<SchemaT extends Schema, TContext = {}>(fieldSchema: SchemaT, schemaOptions: ValidateOptions<TContext> = {}) {
return (val: unknown) => {
try {
fieldSchema.validateSync(
Expand Down
8 changes: 4 additions & 4 deletions src/getters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, test } from 'vitest'
import { describe, expect, it } from 'vitest'
import { nextTick, reactive, watchEffect } from 'vue'
import { useSetup } from '../packages/.test'
import { useDirtyFields, useIsError } from './getters'
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('useDirtyFields', () => {
})
})

test('useForm dirtyFields', () => {
it('useForm dirtyFields', () => {
const wr = useSetup(() => {
const { form, status, reset, dirtyFields, isDirty } = useForm({
form: () => ({
Expand Down Expand Up @@ -117,7 +117,7 @@ test('useForm dirtyFields', () => {

// 确保重制后的修改检查依然是正确的
// Ensure that the modification checks are still correct after the remake
test('useForm reset dirtyFields', async () => {
it('useForm reset dirtyFields', async () => {
const wr = useSetup(() => {
const { form, status, reset, dirtyFields, isDirty } = useForm({
form: () => ({
Expand Down Expand Up @@ -147,7 +147,7 @@ test('useForm reset dirtyFields', async () => {
expect(wr.isDirty).true
})

test('useIsError', () => {
it('useIsError', () => {
expect(useIsError).toBeDefined()

const status = reactive({
Expand Down
10 changes: 5 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('useForm', () => {
})

// 规则验证在第一次值更改后开始工作
it('Rule validation starts working after the first value change', async () => {
it('rule validation starts working after the first value change', async () => {
const wrapper = useSetup(() => {
const { form, status, isError } = useForm({
form: () => ({
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('useForm', () => {
})

// 响应规则更改
it('Responding to rule changes', async () => {
it('responding to rule changes', async () => {
const useI18n = () => {
const local = ref('en')
return {
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('useForm', () => {
})

// 使用数组定义多个规则
it('Define multiple rules using an array', async () => {
it('define multiple rules using an array', async () => {
const wrapper = useSetup(() => {
const { form, status } = useForm({
form: () => ({
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('useForm', () => {
})

// 手动触发验证和清除错误
it('Manually trigger validation and clearErrors', async () => {
it('manually trigger validation and clearErrors', async () => {
const wrapper = useSetup(() => {
const { form, status, clearErrors, verify } = useForm({
form: () => ({
Expand Down Expand Up @@ -364,7 +364,7 @@ describe('useForm', () => {
})

// 自定义表单校验信息占位内容
it('Customize the placeholder content of the validation message', async () => {
it('customize the placeholder content of the validation message', async () => {
const wrapper = useSetup(() => {
const { form, status } = useForm({
form: () => ({
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useDirtyFields, useIsError, useIsFormDirty } from './getters'
import { createSubmitter } from './submitter'
import { initRule } from './rule'

const defaultParam: Required<{ defaultMessage: UseFormDefaultMessage; lazy: UseFormLazy }> = {
const defaultParam: Required<{ defaultMessage: UseFormDefaultMessage, lazy: UseFormLazy }> = {
defaultMessage: '',
lazy: false,
}
Expand Down
14 changes: 7 additions & 7 deletions src/submitter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test, vi } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'
import { createSubmit } from './submitter'
import { useForm } from '.'
Expand All @@ -14,7 +14,7 @@ describe('createSubmit', () => {
})
const { form } = formData

test('this should work', () => {
it('this should work', () => {
const submitFn = vi.fn(() => 0)

const { submit } = createSubmit(formData, submitFn)
Expand All @@ -28,7 +28,7 @@ describe('createSubmit', () => {
expect(submitFn).toHaveBeenCalledTimes(1)
})

test('skip rule validation', () => {
it('skip rule validation', () => {
form.field = 1

const submitFn = vi.fn(() => 0)
Expand All @@ -41,7 +41,7 @@ describe('createSubmit', () => {
expect(submitFn).toHaveBeenCalledTimes(1)
})

test('async submit', async () => {
it('async submit', async () => {
form.field = 0

const submitFn = vi.fn(async () => {
Expand All @@ -62,7 +62,7 @@ describe('createSubmit', () => {
expect(submitFn).toHaveBeenCalledOnce()
})

test('close `submitting` after async error', async () => {
it('close `submitting` after async error', async () => {
form.field = 0

const submitFn = vi.fn(async () => {
Expand All @@ -85,7 +85,7 @@ describe('createSubmit', () => {
})

describe('submitter in useForm', () => {
test('this should work', async () => {
it('this should work', async () => {
const { form, submitter } = useForm({
form: () => ({
field: 0,
Expand All @@ -110,7 +110,7 @@ describe('submitter in useForm', () => {
expect(submitting.value).false
})

test('contains various parameters', async () => {
it('contains various parameters', async () => {
const { form, submitter } = useForm({
form: () => ({
field: 0,
Expand Down
8 changes: 3 additions & 5 deletions src/type/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ export interface UseFormParam<FormT> {
* Unless `verify()` or `status[fieldName].verify()` is called manually to validate the rule.
*
* @default false
* */
*/
lazy?: UseFormLazy
}

export interface ValidateOptions<FullResult extends boolean> { fullResult?: FullResult }
export interface ValidateResult { valid: boolean; message: string }
export interface ValidateResult { valid: boolean, message: string }

export interface UseFormReturnRuleItem {
validate(value: any): boolean
validate(value: any, options: ValidateOptions<false>): boolean
validate(value: any, options: ValidateOptions<true>): ValidateResult
validate: ((value: any) => boolean) & ((value: any, options: ValidateOptions<false>) => boolean) & ((value: any, options: ValidateOptions<true>) => ValidateResult)
}
export type UseFormReturnRule<FormT> = Record<keyof UseFormRule<FormT>, UseFormReturnRuleItem>

Expand Down
Loading

0 comments on commit 2f54207

Please sign in to comment.