Skip to content

Commit 792a45b

Browse files
committed
refactor: update validation logic in InputProvider and remove obsolete tests
1 parent f147716 commit 792a45b

File tree

3 files changed

+20
-58
lines changed

3 files changed

+20
-58
lines changed

src/schema/providers/input.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ValidationResult } from "./../validation-result.js";
22
import { BaseProvider } from "./base-provider.js";
3-
import { validate } from "../validation.js";
43

54
const TEMPLATE = `
65
<label data-field="__field__">
@@ -31,12 +30,11 @@ export class InputProvider extends BaseProvider {
3130
* @param path {String} The path of the schema part
3231
* @returns {ValidationResult} True if the schema is valid, false otherwise.
3332
*/
34-
static async validate(schemaItem, path) {
35-
return validate(schemaItem, {
36-
field: { type: "string", critical: true },
37-
title: { type: "string", critical: true },
38-
placeholder: { type: "string" }
39-
}, path);
33+
static validate(schemaItem, path) {
34+
return ValidationResult.from([
35+
{ condition: is.empty(schemaItem.field), message: "Field is required" },
36+
{ condition: is.empty(schemaItem.type), message: "Type is required" },
37+
], "InputProvider", path);
4038
}
4139

4240
/**

src/schema/validation-result.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,19 @@ export class ValidationResult {
5151
static success(message, path = "") {
5252
return Object.freeze({ type: SUCCESS, message, path });
5353
}
54+
55+
static from(conditions, context, data) {
56+
const errors = [];
57+
for (const condition of conditions) {
58+
if (!assert(condition.condition, condition.message, context, data)) {
59+
errors.push(ValidationResult.error(condition.message, data));
60+
}
61+
}
62+
63+
if (errors.length > 0) {
64+
return this.error(errors.map(e => e.message).join("\n"), data);
65+
}
66+
67+
return this.success("success", data);
68+
}
5469
}

tests/src/schema/validation.test.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)