Skip to content

Commit 8ddb191

Browse files
committed
Adds ajv schema validation for acmesh dns provider meta data fields and formik error messages
1 parent cd6b772 commit 8ddb191

File tree

3 files changed

+59
-34
lines changed

3 files changed

+59
-34
lines changed

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@types/styled-components": "5.1.25",
2020
"@typescript-eslint/eslint-plugin": "^5.30.6",
2121
"@typescript-eslint/parser": "^5.30.6",
22+
"ajv": "^8.12.0",
2223
"babel-eslint": "^10.1.0",
2324
"classnames": "^2.3.1",
2425
"country-flag-icons": "^1.5.5",

frontend/src/modals/DNSProviderCreateModal.tsx

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import {
1818
Stack,
1919
useToast,
2020
} from "@chakra-ui/react";
21+
import Ajv, { Schema } from "ajv";
2122
import {
2223
DNSProvider,
2324
DNSProvidersAcmesh,
2425
DNSProvidersAcmeshProperty,
2526
} from "api/npm";
2627
import { PrettyButton } from "components";
27-
import { Formik, Form, Field } from "formik";
28+
import { Formik, Form, Field, getIn } from "formik";
2829
import { useSetDNSProvider, useDNSProvidersAcmesh } from "hooks";
2930
import { intl } from "locale";
3031
import { validateString } from "modules/Validations";
@@ -39,12 +40,8 @@ function DNSProviderCreateModal({
3940
}: DNSProviderCreateModalProps) {
4041
const toast = useToast();
4142
const { mutate: setDNSProvider } = useSetDNSProvider();
42-
const {
43-
isLoading: acmeshIsLoading,
44-
// isError: acmeshIsError,
45-
// error: acmeshError,
46-
data: acmeshDataResp,
47-
} = useDNSProvidersAcmesh();
43+
const { isLoading: acmeshIsLoading, data: acmeshDataResp } =
44+
useDNSProvidersAcmesh();
4845

4946
const [acmeshData, setAcmeshData] = useState([] as DNSProvidersAcmesh[]);
5047
const [acmeshItem, setAcmeshItem] = useState("");
@@ -58,13 +55,17 @@ function DNSProviderCreateModal({
5855
onClose();
5956
};
6057

58+
const getAcmeshItem = (name: string): DNSProvidersAcmesh | undefined => {
59+
return acmeshData.find((item) => item.title === name);
60+
};
61+
62+
const fullItem = getAcmeshItem(acmeshItem);
63+
const itemProperties = fullItem?.properties;
64+
6165
const onSubmit = async (
6266
payload: DNSProvider,
6367
{ setErrors, setSubmitting }: any,
6468
) => {
65-
// TODO: filter out the meta object and only include items that apply to the acmesh provider selected
66-
console.log("PAYLOAD:", payload);
67-
6869
const showErr = (msg: string) => {
6970
toast({
7071
description: intl.formatMessage({
@@ -77,30 +78,42 @@ function DNSProviderCreateModal({
7778
});
7879
};
7980

80-
setDNSProvider(payload, {
81-
onError: (err: any) => {
82-
if (err.message === "ca-bundle-does-not-exist") {
83-
setErrors({
84-
caBundle: intl.formatMessage({
85-
id: `error.${err.message}`,
86-
}),
87-
});
88-
} else {
89-
showErr(err.message);
90-
}
91-
},
92-
onSuccess: () => onModalClose(),
93-
onSettled: () => setSubmitting(false),
94-
});
95-
};
96-
97-
const getAcmeshItem = (name: string): DNSProvidersAcmesh | undefined => {
98-
return acmeshData.find((item) => item.title === name);
81+
const ajv = new Ajv({ strictSchema: false });
82+
try {
83+
const valid = ajv.validate(fullItem as Schema, payload.meta);
84+
if (!valid) {
85+
let errs: any = {};
86+
ajv.errors?.forEach((e: any) => {
87+
errs["meta"] = {
88+
[e.instancePath.substring(1)]: e.message,
89+
};
90+
});
91+
setErrors(errs);
92+
setSubmitting(false);
93+
} else {
94+
// Json schema is happy
95+
setDNSProvider(payload, {
96+
onError: (err: any) => {
97+
if (err.message === "ca-bundle-does-not-exist") {
98+
setErrors({
99+
caBundle: intl.formatMessage({
100+
id: `error.${err.message}`,
101+
}),
102+
});
103+
} else {
104+
showErr(err.message);
105+
}
106+
},
107+
onSuccess: () => onModalClose(),
108+
onSettled: () => setSubmitting(false),
109+
});
110+
}
111+
} catch (err: any) {
112+
showErr(err);
113+
setSubmitting(false);
114+
}
99115
};
100116

101-
const fullItem = getAcmeshItem(acmeshItem);
102-
const itemProperties = fullItem?.properties;
103-
104117
const renderInputType = (
105118
field: any,
106119
fieldName: string,
@@ -250,7 +263,8 @@ function DNSProviderCreateModal({
250263
) !== -1
251264
}
252265
isInvalid={
253-
form.errors[name] && form.touched[name]
266+
getIn(form.errors, name) &&
267+
getIn(form.touched, name)
254268
}>
255269
{f.type !== "bool" ? (
256270
<FormLabel htmlFor={name}>
@@ -266,7 +280,7 @@ function DNSProviderCreateModal({
266280
values.meta[f.title],
267281
)}
268282
<FormErrorMessage>
269-
{form.errors[name]}
283+
{form.errors?.meta?.[fieldName]}
270284
</FormErrorMessage>
271285
</FormControl>
272286
)}

frontend/yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,6 +3584,16 @@ ajv@^8.0.0, ajv@^8.6.0, ajv@^8.8.0:
35843584
require-from-string "^2.0.2"
35853585
uri-js "^4.2.2"
35863586

3587+
ajv@^8.12.0:
3588+
version "8.12.0"
3589+
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.12.0.tgz#d1a0527323e22f53562c567c00991577dfbe19d1"
3590+
integrity sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==
3591+
dependencies:
3592+
fast-deep-equal "^3.1.1"
3593+
json-schema-traverse "^1.0.0"
3594+
require-from-string "^2.0.2"
3595+
uri-js "^4.2.2"
3596+
35873597
ansi-escapes@^4.2.1, ansi-escapes@^4.3.1:
35883598
version "4.3.2"
35893599
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-4.3.2.tgz#6b2291d1db7d98b6521d5f1efa42d0f3a9feb65e"

0 commit comments

Comments
 (0)