Skip to content

Commit

Permalink
allow validation of single fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaneri committed Apr 6, 2022
1 parent 569c2cc commit fc8e4bb
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 59 deletions.
12 changes: 7 additions & 5 deletions dist/reform.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,12 @@ function useReform(fields) {
return __spreadProps(__spreadValues({}, carry), { [key]: fields[key].validate || null });
}, {});
const validate = () => {
return Object.keys(validators).reduce((carry, key) => {
const validator = validators[key];
errors[key] = (Array.isArray(validator) ? validator : [validator]).map((validator2) => typeof validator2 === "function" ? validator2(data[key], data) : validator2).find((valid) => typeof valid === "string");
return carry && !errors[key];
}, true);
return Object.keys(validators).reduce((carry, key) => carry && validateField(key), true);
};
const validateField = (name) => {
const validator = validators[name];
errors[name] = (Array.isArray(validator) ? validator : [validator]).map((validator2) => typeof validator2 === "function" ? validator2(data[name], data) : validator2).find((valid) => typeof valid === "string");
return !errors[name];
};
return reactive({
data,
Expand All @@ -1050,6 +1051,7 @@ function useReform(fields) {
rollback,
errors,
validate,
validateField,
dirty: computed(() => Object.keys(changes.value).length > 0),
addErrors: (newErrors) => {
Object.entries(newErrors).forEach(([name, messages]) => {
Expand Down
2 changes: 1 addition & 1 deletion dist/reform.umd.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/types/reform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare function useReform<T extends {
[x: string]: ValidatorError;
};
validate: () => boolean;
validateField: (name: string) => boolean;
dirty: boolean;
addErrors: (newErrors: {
[key: string]: string | string[];
Expand Down
2 changes: 1 addition & 1 deletion dist/vite.config.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare const _default: import("vite").UserConfigExport;
declare const _default: () => import("vite").UserConfigExport;
export default _default;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tmaneri/reform",
"version": "0.0.1",
"version": "0.0.2",
"files": [
"dist"
],
Expand All @@ -20,7 +20,8 @@
"build": "vite build && vue-tsc --emitDeclarationOnly && mv dist/src dist/types",
"preserve": "vite build",
"serve": "vite preview --port 5050",
"typecheck": "vue-tsc --noEmit"
"typecheck": "vue-tsc --noEmit",
"report": "export BUILD_REPORT=1 && npm run build && open ./stats.html"
},
"dependencies": {
"@types/lodash-es": "^4.17.6",
Expand All @@ -36,6 +37,7 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-vue": "^8.5.0",
"prettier": "^2.6.1",
"rollup-plugin-visualizer": "^5.6.0",
"typescript": "~4.3.5",
"vite": "^2.6.3",
"vue": "^3.2.20",
Expand Down
31 changes: 18 additions & 13 deletions src/reform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ export function useReform<T extends { [x: string]: FieldValue }>(fields: {
}, {});

const validate = () => {
return Object.keys(validators).reduce((carry, key) => {
const validator = validators[key];

errors[key] = (Array.isArray(validator) ? validator : [validator])
.map((validator) =>
typeof validator === "function"
? validator(data[key], data)
: validator
)
.find((valid) => typeof valid === "string");

return carry && !errors[key];
}, true);
return Object.keys(validators).reduce(
(carry, key) => carry && validateField(key),
true
);
};

const validateField = (name: string) => {
const validator = validators[name];
errors[name] = (Array.isArray(validator) ? validator : [validator])
.map((validator) =>
typeof validator === "function"
? validator(data[name], data)
: validator
)
.find((valid) => typeof valid === "string");

return !errors[name];
};

return reactive({
Expand All @@ -75,6 +79,7 @@ export function useReform<T extends { [x: string]: FieldValue }>(fields: {
rollback,
errors,
validate,
validateField,

dirty: computed(() => Object.keys(changes.value).length > 0),

Expand Down
53 changes: 31 additions & 22 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,37 @@ import { resolve } from "path";

const packageName = process.env.npm_package_name;

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
dedupe: ["vue"],
},
build: {
lib: {
entry: resolve(__dirname, "src/index.ts"),
name: packageName,
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ["vue"],
output: {
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
vue: "Vue",
import { visualizer } from "rollup-plugin-visualizer";

export default () => {
const plugins = [vue()];

if (process.env.BUILD_REPORT) {
plugins.push(visualizer());
}

return defineConfig({
plugins,
// resolve: {
// dedupe: ["vue"],
// },
build: {
lib: {
entry: resolve(__dirname, "src/index.ts"),
name: packageName,
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ["vue"],
output: {
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
vue: "Vue",
},
},
},
},
},
});
});
};
Loading

0 comments on commit fc8e4bb

Please sign in to comment.