mirror of
https://github.com/jc21/nginx-proxy-manager.git
synced 2024-08-30 18:22:48 +00:00
Adds ajv schema validation for acmesh dns provider meta data fields and formik error messages
This commit is contained in:
parent
cd6b772e72
commit
8ddb191a7a
@ -19,6 +19,7 @@
|
|||||||
"@types/styled-components": "5.1.25",
|
"@types/styled-components": "5.1.25",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.30.6",
|
"@typescript-eslint/eslint-plugin": "^5.30.6",
|
||||||
"@typescript-eslint/parser": "^5.30.6",
|
"@typescript-eslint/parser": "^5.30.6",
|
||||||
|
"ajv": "^8.12.0",
|
||||||
"babel-eslint": "^10.1.0",
|
"babel-eslint": "^10.1.0",
|
||||||
"classnames": "^2.3.1",
|
"classnames": "^2.3.1",
|
||||||
"country-flag-icons": "^1.5.5",
|
"country-flag-icons": "^1.5.5",
|
||||||
|
@ -18,13 +18,14 @@ import {
|
|||||||
Stack,
|
Stack,
|
||||||
useToast,
|
useToast,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
|
import Ajv, { Schema } from "ajv";
|
||||||
import {
|
import {
|
||||||
DNSProvider,
|
DNSProvider,
|
||||||
DNSProvidersAcmesh,
|
DNSProvidersAcmesh,
|
||||||
DNSProvidersAcmeshProperty,
|
DNSProvidersAcmeshProperty,
|
||||||
} from "api/npm";
|
} from "api/npm";
|
||||||
import { PrettyButton } from "components";
|
import { PrettyButton } from "components";
|
||||||
import { Formik, Form, Field } from "formik";
|
import { Formik, Form, Field, getIn } from "formik";
|
||||||
import { useSetDNSProvider, useDNSProvidersAcmesh } from "hooks";
|
import { useSetDNSProvider, useDNSProvidersAcmesh } from "hooks";
|
||||||
import { intl } from "locale";
|
import { intl } from "locale";
|
||||||
import { validateString } from "modules/Validations";
|
import { validateString } from "modules/Validations";
|
||||||
@ -39,12 +40,8 @@ function DNSProviderCreateModal({
|
|||||||
}: DNSProviderCreateModalProps) {
|
}: DNSProviderCreateModalProps) {
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
const { mutate: setDNSProvider } = useSetDNSProvider();
|
const { mutate: setDNSProvider } = useSetDNSProvider();
|
||||||
const {
|
const { isLoading: acmeshIsLoading, data: acmeshDataResp } =
|
||||||
isLoading: acmeshIsLoading,
|
useDNSProvidersAcmesh();
|
||||||
// isError: acmeshIsError,
|
|
||||||
// error: acmeshError,
|
|
||||||
data: acmeshDataResp,
|
|
||||||
} = useDNSProvidersAcmesh();
|
|
||||||
|
|
||||||
const [acmeshData, setAcmeshData] = useState([] as DNSProvidersAcmesh[]);
|
const [acmeshData, setAcmeshData] = useState([] as DNSProvidersAcmesh[]);
|
||||||
const [acmeshItem, setAcmeshItem] = useState("");
|
const [acmeshItem, setAcmeshItem] = useState("");
|
||||||
@ -58,13 +55,17 @@ function DNSProviderCreateModal({
|
|||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getAcmeshItem = (name: string): DNSProvidersAcmesh | undefined => {
|
||||||
|
return acmeshData.find((item) => item.title === name);
|
||||||
|
};
|
||||||
|
|
||||||
|
const fullItem = getAcmeshItem(acmeshItem);
|
||||||
|
const itemProperties = fullItem?.properties;
|
||||||
|
|
||||||
const onSubmit = async (
|
const onSubmit = async (
|
||||||
payload: DNSProvider,
|
payload: DNSProvider,
|
||||||
{ setErrors, setSubmitting }: any,
|
{ setErrors, setSubmitting }: any,
|
||||||
) => {
|
) => {
|
||||||
// TODO: filter out the meta object and only include items that apply to the acmesh provider selected
|
|
||||||
console.log("PAYLOAD:", payload);
|
|
||||||
|
|
||||||
const showErr = (msg: string) => {
|
const showErr = (msg: string) => {
|
||||||
toast({
|
toast({
|
||||||
description: intl.formatMessage({
|
description: intl.formatMessage({
|
||||||
@ -77,6 +78,20 @@ function DNSProviderCreateModal({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const ajv = new Ajv({ strictSchema: false });
|
||||||
|
try {
|
||||||
|
const valid = ajv.validate(fullItem as Schema, payload.meta);
|
||||||
|
if (!valid) {
|
||||||
|
let errs: any = {};
|
||||||
|
ajv.errors?.forEach((e: any) => {
|
||||||
|
errs["meta"] = {
|
||||||
|
[e.instancePath.substring(1)]: e.message,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
setErrors(errs);
|
||||||
|
setSubmitting(false);
|
||||||
|
} else {
|
||||||
|
// Json schema is happy
|
||||||
setDNSProvider(payload, {
|
setDNSProvider(payload, {
|
||||||
onError: (err: any) => {
|
onError: (err: any) => {
|
||||||
if (err.message === "ca-bundle-does-not-exist") {
|
if (err.message === "ca-bundle-does-not-exist") {
|
||||||
@ -92,15 +107,13 @@ function DNSProviderCreateModal({
|
|||||||
onSuccess: () => onModalClose(),
|
onSuccess: () => onModalClose(),
|
||||||
onSettled: () => setSubmitting(false),
|
onSettled: () => setSubmitting(false),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
showErr(err);
|
||||||
|
setSubmitting(false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAcmeshItem = (name: string): DNSProvidersAcmesh | undefined => {
|
|
||||||
return acmeshData.find((item) => item.title === name);
|
|
||||||
};
|
|
||||||
|
|
||||||
const fullItem = getAcmeshItem(acmeshItem);
|
|
||||||
const itemProperties = fullItem?.properties;
|
|
||||||
|
|
||||||
const renderInputType = (
|
const renderInputType = (
|
||||||
field: any,
|
field: any,
|
||||||
fieldName: string,
|
fieldName: string,
|
||||||
@ -250,7 +263,8 @@ function DNSProviderCreateModal({
|
|||||||
) !== -1
|
) !== -1
|
||||||
}
|
}
|
||||||
isInvalid={
|
isInvalid={
|
||||||
form.errors[name] && form.touched[name]
|
getIn(form.errors, name) &&
|
||||||
|
getIn(form.touched, name)
|
||||||
}>
|
}>
|
||||||
{f.type !== "bool" ? (
|
{f.type !== "bool" ? (
|
||||||
<FormLabel htmlFor={name}>
|
<FormLabel htmlFor={name}>
|
||||||
@ -266,7 +280,7 @@ function DNSProviderCreateModal({
|
|||||||
values.meta[f.title],
|
values.meta[f.title],
|
||||||
)}
|
)}
|
||||||
<FormErrorMessage>
|
<FormErrorMessage>
|
||||||
{form.errors[name]}
|
{form.errors?.meta?.[fieldName]}
|
||||||
</FormErrorMessage>
|
</FormErrorMessage>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
)}
|
)}
|
||||||
|
@ -3584,6 +3584,16 @@ ajv@^8.0.0, ajv@^8.6.0, ajv@^8.8.0:
|
|||||||
require-from-string "^2.0.2"
|
require-from-string "^2.0.2"
|
||||||
uri-js "^4.2.2"
|
uri-js "^4.2.2"
|
||||||
|
|
||||||
|
ajv@^8.12.0:
|
||||||
|
version "8.12.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.12.0.tgz#d1a0527323e22f53562c567c00991577dfbe19d1"
|
||||||
|
integrity sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==
|
||||||
|
dependencies:
|
||||||
|
fast-deep-equal "^3.1.1"
|
||||||
|
json-schema-traverse "^1.0.0"
|
||||||
|
require-from-string "^2.0.2"
|
||||||
|
uri-js "^4.2.2"
|
||||||
|
|
||||||
ansi-escapes@^4.2.1, ansi-escapes@^4.3.1:
|
ansi-escapes@^4.2.1, ansi-escapes@^4.3.1:
|
||||||
version "4.3.2"
|
version "4.3.2"
|
||||||
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-4.3.2.tgz#6b2291d1db7d98b6521d5f1efa42d0f3a9feb65e"
|
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-4.3.2.tgz#6b2291d1db7d98b6521d5f1efa42d0f3a9feb65e"
|
||||||
|
Loading…
Reference in New Issue
Block a user