Skip to content

Commit 28e9fd3

Browse files
authored
feat(useValidation): add toObject (pikax#723)
1 parent 3d73368 commit 28e9fd3

File tree

6 files changed

+219
-66
lines changed

6 files changed

+219
-66
lines changed

docs/.vuepress/components/ValidationExample.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export default defineComponent({
8484
if (form.$anyInvalid) {
8585
alert("invalid form");
8686
} else {
87-
alert("submit form");
87+
const o = form.toObject();
88+
alert(`submit form "${JSON.stringify(o)}"`);
89+
console.log("submitted", o);
8890
}
8991
};
9092

docs/composable/validation/validation.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ interface ValidationValue<T> {
117117

118118
$anyInvalid: boolean; // any validation invalid
119119
$errors: any[]; // array of errors
120+
121+
toObject(): T;
120122
}
121123

122124
// validator
@@ -159,6 +161,9 @@ validationUsername.containsInvalidWords.$customProp; //custom prop
159161

160162
// custom properties
161163
validationUsername.$placeholder; // custom prop
164+
165+
// retrieve value object
166+
validationUsername.toObject(); // returns string
162167
```
163168

164169
### NestedValidationObject
@@ -205,6 +210,8 @@ form.settings.$errors;
205210
form.personal.$anyDirty;
206211
form.personal.$anyInvalid;
207212
form.personal.$errors;
213+
214+
form.toObject(); // returns { settings: { email: '' }, personal: { name: { first: '', last: '' } } }
208215
```
209216

210217
```ts
@@ -308,7 +315,9 @@ export default defineComponent({
308315
if (form.$anyInvalid) {
309316
alert("invalid form");
310317
} else {
311-
alert("submit form");
318+
const o = form.toObject();
319+
alert(`submit form "${JSON.stringify(o)}"`);
320+
console.log("submitted", o);
312321
}
313322
};
314323

packages/axios/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,9 @@
6060
"@types/node": "^14.14.20",
6161
"@vue/runtime-core": "^3.0.5",
6262
"typescript": "^4.1.3"
63+
},
64+
"peerDependencies": {
65+
"@vue/composition-api": "^1.0.0-beta.14",
66+
"axios": "^0.22.0"
6367
}
6468
}

packages/vue-composable/__tests__/validation/validation.spec.ts

Lines changed: 131 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ describe("validation", () => {
1212
$value,
1313
required(x: string) {
1414
return !!x;
15-
}
15+
},
1616
},
1717
test1: {
1818
$value: $value1,
1919
required(x: string) {
2020
return !!x;
21-
}
22-
}
21+
},
22+
},
2323
});
2424

2525
expect(validation).toMatchObject({
@@ -30,16 +30,16 @@ describe("validation", () => {
3030
$dirty: false,
3131
$value: $value.value,
3232
required: {
33-
$invalid: true
34-
}
33+
$invalid: true,
34+
},
3535
},
3636
test1: {
3737
$dirty: false,
3838
$value: $value1.value,
3939
required: {
40-
$invalid: true
41-
}
42-
}
40+
$invalid: true,
41+
},
42+
},
4343
});
4444

4545
$value.value = "hello";
@@ -55,31 +55,31 @@ describe("validation", () => {
5555
$dirty: true,
5656
$value: $value.value,
5757
required: {
58-
$invalid: false
59-
}
58+
$invalid: false,
59+
},
6060
},
6161
test1: {
6262
$dirty: false,
6363
$value: $value1.value,
6464
required: {
65-
$invalid: true
66-
}
67-
}
65+
$invalid: true,
66+
},
67+
},
6868
});
6969
});
7070

7171
it("validator should run if dependent of other ref", async () => {
7272
const password = ref("");
7373
const form = useValidation({
7474
password: {
75-
$value: password
75+
$value: password,
7676
},
7777
password2: {
7878
$value: ref(""),
7979
samePassword(r: string, ctx: any) {
8080
return r === ctx.password.$value;
81-
}
82-
}
81+
},
82+
},
8383
});
8484

8585
expect(form.password2.samePassword.$invalid).toBe(false);
@@ -95,8 +95,8 @@ describe("validation", () => {
9595
const v = useValidation({
9696
password: {
9797
$value: ref(""),
98-
$args
99-
}
98+
$args,
99+
},
100100
});
101101

102102
expect(v.password.$args).toStrictEqual($args);
@@ -109,8 +109,8 @@ describe("validation", () => {
109109
$value: ref(""),
110110
required() {
111111
throw error;
112-
}
113-
}
112+
},
113+
},
114114
});
115115

116116
await nextTick();
@@ -122,15 +122,17 @@ describe("validation", () => {
122122

123123
it("should handle promise validator", async () => {
124124
let promiseResolve: Function = NO_OP;
125-
const promise = new Promise<boolean>(resolve => (promiseResolve = resolve));
125+
const promise = new Promise<boolean>(
126+
(resolve) => (promiseResolve = resolve)
127+
);
126128

127129
const v = useValidation({
128130
password: {
129131
$value: ref(""),
130132
required() {
131133
return promise;
132-
}
133-
}
134+
},
135+
},
134136
});
135137

136138
expect(v.password.required.$pending).toBe(true);
@@ -143,7 +145,9 @@ describe("validation", () => {
143145

144146
it("should handle promise validator with objectValidator", async () => {
145147
let promiseResolve: Function = NO_OP;
146-
const promise = new Promise<boolean>(resolve => (promiseResolve = resolve));
148+
const promise = new Promise<boolean>(
149+
(resolve) => (promiseResolve = resolve)
150+
);
147151

148152
const v = useValidation({
149153
password: {
@@ -152,9 +156,9 @@ describe("validation", () => {
152156
$validator() {
153157
return promise;
154158
},
155-
$message: ref("Err")
156-
}
157-
}
159+
$message: ref("Err"),
160+
},
161+
},
158162
});
159163

160164
v.password.$value;
@@ -178,15 +182,15 @@ describe("validation", () => {
178182
},
179183
match() {
180184
throw Error("error 2");
181-
}
182-
}
185+
},
186+
},
183187
});
184188
v.input.$value = "1";
185189
await nextTick();
186190

187191
expect(v.input.$errors).toStrictEqual([
188192
new Error("error 1"),
189-
new Error("error 2")
193+
new Error("error 2"),
190194
]);
191195
});
192196

@@ -199,21 +203,109 @@ describe("validation", () => {
199203
$validator(x: string) {
200204
return false;
201205
},
202-
$message
203-
}
206+
$message,
207+
},
204208
},
205209
otherInput: {
206210
$value: "",
207211
required(x: string) {
208212
return false;
209-
}
210-
}
213+
},
214+
},
211215
});
212216

213217
expect(v.input.$errors).toMatchObject([$message]);
214218
expect(v.otherInput.$errors).toMatchObject([]);
215219
});
216220

221+
describe("object", () => {
222+
it("should convert to object", () => {
223+
const v = useValidation({
224+
input: {
225+
$value: "",
226+
required: {
227+
$validator(x: string) {
228+
return false;
229+
},
230+
$message: "test",
231+
},
232+
},
233+
otherInput: {
234+
$value: "",
235+
required(x: string) {
236+
return false;
237+
},
238+
},
239+
});
240+
241+
expect(v.toObject()).toMatchObject({ input: "", otherInput: "" });
242+
243+
v.input.$value = "test";
244+
v.otherInput.$value = "other";
245+
246+
expect(v.toObject()).toMatchObject({
247+
input: "test",
248+
otherInput: "other",
249+
});
250+
251+
expect(v.input.toObject()).toBe("test");
252+
});
253+
254+
it("should convert to object even on nested validations", () => {
255+
const v = useValidation({
256+
address: {
257+
address1: {
258+
$value: "address1",
259+
},
260+
address2: {
261+
part1: {
262+
$value: "part1",
263+
},
264+
part2: {
265+
$value: "part2",
266+
},
267+
},
268+
},
269+
});
270+
expect(v.toObject()).toMatchObject({
271+
address: {
272+
address1: "address1",
273+
address2: {
274+
part1: "part1",
275+
part2: "part2",
276+
},
277+
},
278+
});
279+
280+
v.address.address2.part1.$value = "1";
281+
282+
expect(v.toObject()).toMatchObject({
283+
address: {
284+
address1: "address1",
285+
address2: {
286+
part1: "1",
287+
part2: "part2",
288+
},
289+
},
290+
});
291+
292+
expect(v.address.toObject()).toMatchObject({
293+
address1: "address1",
294+
address2: {
295+
part1: "1",
296+
part2: "part2",
297+
},
298+
});
299+
300+
expect(v.address.address1.toObject()).toBe(v.address.address1.$value);
301+
302+
expect(v.address.address2.toObject()).toMatchObject({
303+
part1: "1",
304+
part2: "part2",
305+
});
306+
});
307+
});
308+
217309
describe("render", () => {
218310
it("should show error", async () => {
219311
const required = (x: any) => !!x;
@@ -223,9 +315,9 @@ describe("validation", () => {
223315
required,
224316
otherRequired: {
225317
$validator: required,
226-
$message: ref("password is required")
227-
}
228-
}
318+
$message: ref("password is required"),
319+
},
320+
},
229321
});
230322

231323
const { mount } = createVue({
@@ -245,9 +337,9 @@ describe("validation", () => {
245337
`,
246338
setup() {
247339
return {
248-
form
340+
form,
249341
};
250-
}
342+
},
251343
});
252344

253345
const vm = mount();

packages/vue-composable/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@
5252
"peerDependencies2": {
5353
"@vue/composition-api": "^1.0.0-beta.14",
5454
"vue": "^2.6.10"
55+
},
56+
"peerDependencies": {
57+
"@vue/composition-api": "^1.0.0-beta.14",
58+
"vue": "^2.6.10"
5559
}
5660
}

0 commit comments

Comments
 (0)