@sealcode/sealgen 0.15.17 → 0.15.18

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.
@@ -15,16 +15,20 @@ class FormField {
15
15
  this.name = fieldname;
16
16
  }
17
17
  async _validate(ctx, value) {
18
- if (this.required && (value === "" || value === null || value === void 0)) {
19
- return {
20
- valid: false,
21
- message: t(
22
- ctx,
23
- "field_is_required",
24
- [],
25
- "This field is required"
26
- )
27
- };
18
+ if (value === "" || value === null || value === void 0) {
19
+ if (this.required) {
20
+ return {
21
+ valid: false,
22
+ message: t(
23
+ ctx,
24
+ "field_is_required",
25
+ [],
26
+ "This field is required"
27
+ )
28
+ };
29
+ } else {
30
+ return { valid: true, message: "Empty, but not required" };
31
+ }
28
32
  }
29
33
  return this.isValueValid(ctx, value);
30
34
  }
@@ -4,6 +4,7 @@ import Koa from "koa";
4
4
  import { mount } from "../../mount.js";
5
5
  import { Form } from "../form.js";
6
6
  import { getBrowser } from "../../utils/browser-creator.js";
7
+ import assert from "assert";
7
8
  import { SimpleFormField } from "./simple-form-field.js";
8
9
  import { SimpleInput } from "../controls/simple-input.js";
9
10
  describe("basic field", () => {
@@ -53,6 +54,46 @@ describe("basic field", () => {
53
54
  page = await context.newPage();
54
55
  await page.goto(`http://localhost:${port}`);
55
56
  });
57
+ it("Doesn't even run `isValueValid` if the value is empty", async () => {
58
+ const port = await getPort();
59
+ console.log(`Using port ${port} form checkboxed-list-input.test.ts`);
60
+ const app = new Koa();
61
+ const router = new Router();
62
+ let is_value_valid_called = false;
63
+ const fields = {
64
+ text: new class MockField extends SimpleFormField {
65
+ async isValueValid() {
66
+ is_value_valid_called = true;
67
+ return true;
68
+ }
69
+ }(false)
70
+ };
71
+ mount(
72
+ router,
73
+ "/",
74
+ new class extends Form {
75
+ constructor() {
76
+ super(...arguments);
77
+ this.fields = fields;
78
+ this.controls = [new SimpleInput(fields.text)];
79
+ }
80
+ async onSubmit() {
81
+ }
82
+ async canAccess(_) {
83
+ return { canAccess: true, message: "" };
84
+ }
85
+ }(),
86
+ true
87
+ );
88
+ app.use(router.routes()).use(router.allowedMethods());
89
+ server = app.listen(port);
90
+ browser = await getBrowser();
91
+ const context = await browser.newContext();
92
+ page = await context.newPage();
93
+ await page.goto(`http://localhost:${port}`);
94
+ await page.getByRole("button", { name: "Wy\u015Blij" }).click();
95
+ assert(is_value_valid_called == false);
96
+ });
56
97
  });
57
98
  });
58
99
  //# sourceMappingURL=field.test.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.15.17",
3
+ "version": "0.15.18",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -10,6 +10,7 @@ import assert from "assert";
10
10
  import { SimpleFormField } from "./simple-form-field.js";
11
11
  import { SimpleInput } from "../controls/simple-input.js";
12
12
  import { FormData } from "../form-types.js";
13
+ import { Field } from "sealious";
13
14
 
14
15
  describe("basic field", () => {
15
16
  describe("basic tests", async () => {
@@ -63,5 +64,50 @@ describe("basic field", () => {
63
64
  page = await context.newPage();
64
65
  await page.goto(`http://localhost:${port}`);
65
66
  });
67
+
68
+ it("Doesn't even run `isValueValid` if the value is empty", async () => {
69
+ const port = await getPort();
70
+ console.log(`Using port ${port} form checkboxed-list-input.test.ts`);
71
+ const app = new Koa();
72
+ const router = new Router();
73
+
74
+ let is_value_valid_called = false;
75
+
76
+ const fields = {
77
+ text: new (class MockField extends SimpleFormField<false> {
78
+ public async isValueValid() {
79
+ is_value_valid_called = true;
80
+ return true as any;
81
+ }
82
+ })(false),
83
+ };
84
+
85
+ mount(
86
+ router,
87
+ "/",
88
+ new (class extends Form<typeof fields, unknown> {
89
+ fields = fields;
90
+ controls = [new SimpleInput(fields.text)];
91
+
92
+ async onSubmit() {
93
+ //noop
94
+ }
95
+
96
+ async canAccess(_: Context) {
97
+ return { canAccess: true, message: "" };
98
+ }
99
+ })(),
100
+ true
101
+ );
102
+ app.use(router.routes()).use(router.allowedMethods());
103
+ server = app.listen(port);
104
+
105
+ browser = await getBrowser();
106
+ const context = await browser.newContext();
107
+ page = await context.newPage();
108
+ await page.goto(`http://localhost:${port}`);
109
+ await page.getByRole("button", { name: "Wyślij" }).click();
110
+ assert(is_value_valid_called == false);
111
+ });
66
112
  });
67
113
  });
@@ -64,20 +64,21 @@ export abstract class FormField<
64
64
  ctx: Context,
65
65
  value: ParsedValue
66
66
  ): Promise<FormFieldValidationResponse> {
67
- if (
68
- this.required &&
69
- (value === "" || value === null || value === undefined)
70
- ) {
71
- return {
72
- valid: false,
73
-
74
- message: t(
75
- ctx,
76
- "field_is_required",
77
- [],
78
- "This field is required"
79
- ),
80
- };
67
+ if (value === "" || value === null || value === undefined) {
68
+ if (this.required) {
69
+ return {
70
+ valid: false,
71
+
72
+ message: t(
73
+ ctx,
74
+ "field_is_required",
75
+ [],
76
+ "This field is required"
77
+ ),
78
+ };
79
+ } else {
80
+ return { valid: true, message: "Empty, but not required" };
81
+ }
81
82
  }
82
83
  return this.isValueValid(ctx, value);
83
84
  }