Skip to content

Commit

Permalink
feat: zod driver, svk nested
Browse files Browse the repository at this point in the history
zod driver, svk nested

#404
  • Loading branch information
foxhound87 committed Dec 3, 2023
1 parent d6b9525 commit fab755a
Show file tree
Hide file tree
Showing 23 changed files with 353 additions and 20 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# 6.6.0 (master)
- Introduced ZOD validation driver
- Added SVK support for array of objects
- Fix: #404

# 6.5.0 (master)
- Deprecatad: `setHooks()` & `getHooks()` methods.
- Handle `set()/get()` for `hooks` and `handlers`

- # 6.4.0 (master)
# 6.4.0 (master)
- Feat: ability to define or override hooks after initialization.
- Feat: Introduced `setHooks()` & `getHooks()` methods.

Expand Down
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"validatorjs": "^3.15.1",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2",
"yup": "^0.32.11"
"yup": "^0.32.11",
"zod": "^3.22.4"
}
}
1 change: 1 addition & 0 deletions src/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class Validator implements ValidatorInterface {
dvr: undefined,
svk: undefined,
yup: undefined,
zod: undefined,
};

error: string | null = null;
Expand Down
1 change: 1 addition & 0 deletions src/models/ValidatorInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ValidationPlugins {
dvr?: any;
svk?: any;
yup?: any;
zod?: any;
}

export interface ValidationPluginConstructor {
Expand Down
11 changes: 5 additions & 6 deletions src/validators/SVK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ class SVK implements ValidationPluginInterface {
}

validate(field) {
const data = { [field.path]: field.validatedValue };
const validate = this.validator(this.parseValues(data));
const validate = this.validator(this.parseValues(field.state.form.validatedValues));
// check if is $async schema
if (isPromise(validate)) {
const $p = validate
Expand All @@ -91,7 +90,7 @@ class SVK implements ValidationPluginInterface {
}

handleSyncError(field, errors) {
const fieldErrorObj = this.findError(field.key, errors);
const fieldErrorObj = this.findError(field.path, errors);
// if fieldErrorObj is not undefined, the current field is invalid.
if (_.isUndefined(fieldErrorObj)) return;
// the current field is now invalid
Expand All @@ -117,9 +116,9 @@ class SVK implements ValidationPluginInterface {
return _.find(errors, ({ dataPath }) => {
let $dataPath;
$dataPath = _.trimStart(dataPath, ".");
$dataPath = _.trim($dataPath, "['");
$dataPath = _.trim($dataPath, "']");
return _.includes($dataPath, `${path}`);
$dataPath = _.replace($dataPath, "]", "");
$dataPath = _.replace($dataPath, "[", ".");
return _.includes($dataPath, path);
});
}

Expand Down
55 changes: 55 additions & 0 deletions src/validators/ZOD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import _ from "lodash";
import {
ValidationPluginConstructor,
ValidationPluginInterface,
} from "../models/ValidatorInterface";

class ZOD implements ValidationPluginInterface {

promises = [];

config = null;

state = null;

extend = null;

validator = null;

schema = null;

constructor({
config = {},
state = null,
promises = [],
}: ValidationPluginConstructor) {
this.state = state;
this.promises = promises;
this.extend = config.extend;
this.validator = config.package || config;
this.schema = config.schema;
this.extendValidator();
}

extendValidator(): void {
// extend using "extend" callback
if (typeof this.extend === 'function') {
this.extend({
validator: this.validator,
form: this.state.form,
});
}
}

validate(field): void {
const result = this.schema.safeParse(field.state.form.validatedValues);
if (result.success) return;
const errors = _.get(result.error.format(), field.path)._errors;
if (errors.length) field.validationErrorStack = errors;
}
}

export default (config?: any) => ({
class: ZOD,
config,
});
6 changes: 6 additions & 0 deletions tests/data/_.nested.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import $B from "./forms/nested/form.b";
import $C from "./forms/nested/form.c";
import $D from "./forms/nested/form.d_";
import $E from "./forms/nested/form.e";
import $E2 from "./forms/nested/form.e2";
import $F from "./forms/nested/form.f";
import $G from "./forms/nested/form.g";
import $H from "./forms/nested/form.h";
Expand All @@ -30,6 +31,8 @@ import $V2 from "./forms/nested/form.v2";
import $V3 from "./forms/nested/form.v3";
import $V4 from "./forms/nested/form.v4";
import $Z from "./forms/nested/form.z";
import $Z1 from "./forms/nested/form.z1";
import $Z2 from "./forms/nested/form.z2";
import $X from "./forms/nested/form.x";

export default {
Expand All @@ -40,6 +43,7 @@ export default {
$C,
$D,
$E,
$E2,
$F,
$G,
$H,
Expand All @@ -62,5 +66,7 @@ export default {
$V3,
$V4,
$Z,
$Z1,
$Z2,
$X,
};
3 changes: 2 additions & 1 deletion tests/data/forms/fixes/form.u.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import validatorjs from "validatorjs";
import { Form } from "../../../../src";
import { OptionsModel } from "../../../../src/models/OptionsModel";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";
import dvr from "../../../../src/validators/DVR";

const fields = ["from", "to"];
Expand All @@ -15,7 +16,7 @@ const labels = {
to: "TO",
};

const plugins = {
const plugins: ValidationPlugins = {
dvr: dvr(validatorjs),
};

Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.b.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import svkExtend from "../../extension/svk";

import dvr from "../../../../src/validators/DVR";
import svk from "../../../../src/validators/SVK";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const fields = {
username: {
Expand Down Expand Up @@ -33,7 +34,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
dvr: dvr(validatorjs),
svk: svk({
schema,
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Form } from "../../../../src";
import svkExtend from "../../extension/svk";
import svk from "../../../../src/validators/SVK";
import { FormInterface } from "../../../../src/models/FormInterface";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const fields = {
username: {
Expand Down Expand Up @@ -33,7 +34,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
svk: svk({
package: ajv,
extend: svkExtend,
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import svkExtend from "../../extension/svk";
import vjf from "../../../../src/validators/VJF";
import dvr from "../../../../src/validators/DVR";
import svk from "../../../../src/validators/SVK";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const fields = {
username: {
Expand Down Expand Up @@ -45,7 +46,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
vjf: vjf(),
dvr: dvr(validatorjs),
svk: svk({
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.f.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import svkExtend from "../../extension/svk";

import vjf from "../../../../src/validators/VJF";
import svk from "../../../../src/validators/SVK";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const schema = {
type: "object",
Expand All @@ -16,7 +17,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
vjf: vjf(),
svk: svk({
package: ajv,
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ajv from "ajv";
import { Form } from "../../../../src";
import svkExtend from "../../extension/svk";
import svk from "../../../../src/validators/SVK";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const schema = {
type: "object",
Expand Down Expand Up @@ -35,7 +36,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
svk: svk({
package: ajv,
extend: svkExtend,
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.i.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Form } from "../../../../src";
import svk from "../../../../src/validators/SVK";
import { FormInterface } from "../../../../src/models/FormInterface";
import { OptionsModel } from "../../../../src/models/OptionsModel";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const fields = {
username: {
Expand All @@ -28,7 +29,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
svk: svk({
package: ajv,
schema,
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.l.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Form } from "../../../../src";
import svkExtend from "../../extension/svk";
import svk from "../../../../src/validators/SVK";
import { FormInterface } from "../../../../src/models/FormInterface";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const fields = {
username: {
Expand Down Expand Up @@ -39,7 +40,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
svk: svk({
package: ajv,
extend: svkExtend,
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.m.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dvr from "../../../../src/validators/DVR";
import svk from "../../../../src/validators/SVK";
import { FormInterface } from "../../../../src/models/FormInterface";
import { OptionsModel } from "../../../../src/models/OptionsModel";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const fields = {
username: {
Expand Down Expand Up @@ -35,7 +36,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
svk: svk({
package: ajv,
schema,
Expand Down
3 changes: 2 additions & 1 deletion tests/data/forms/flat/form.n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import svk from "../../../../src/validators/SVK";
import vjf from "../../../../src/validators/VJF";
import { FormInterface } from "../../../../src/models/FormInterface";
import { OptionsModel } from "../../../../src/models/OptionsModel";
import { ValidationPlugins } from "../../../../src/models/ValidatorInterface";

const fields = {
username: {
Expand All @@ -32,7 +33,7 @@ const schema = {
},
};

const plugins = {
const plugins: ValidationPlugins = {
vjf: vjf(),
svk: svk({
package: ajv,
Expand Down
Loading

0 comments on commit fab755a

Please sign in to comment.