Skip to content

Commit

Permalink
Merge pull request #700 from CodeForAfrica/fix/charterafrica_array_se…
Browse files Browse the repository at this point in the history
…lect_validate

Fix @charterafrica validate of select in an array field
  • Loading branch information
kilemensi committed May 27, 2024
2 parents fd78167 + 581df65 commit d62e47d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion apps/charterafrica/contrib/dokku/Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FROM codeforafrica/charterafrica-ui:0.1.24
FROM codeforafrica/charterafrica-ui:0.1.25
2 changes: 1 addition & 1 deletion apps/charterafrica/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "charterafrica",
"version": "0.1.24",
"version": "0.1.25",
"private": true,
"author": "Code for Africa <[email protected]>",
"description": "This is the official code for https://charter.africa site",
Expand Down
19 changes: 7 additions & 12 deletions apps/charterafrica/src/payload/blocks/Datasets.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { array } from "payload/dist/fields/validations";

import filterBar from "../fields/filterBar";
import linkGroup from "../fields/linkGroup";
import defaultValue from "../utils/defaultValues";
import validateUniqueArrayFieldSelect from "../utils/validateUniqueArrayFieldSelect";

const sortOptions = [
"metadata_created desc",
Expand Down Expand Up @@ -130,16 +129,12 @@ const Datasets = {
type: "select",
required: true,
options: sortOptions,
validate: (val, options) => {
const { data, t } = options || {};
if (
data?.options?.filter((l) => l.value === val)
?.length > 1
) {
return t("charterafrica.site:uniqueSortOptions");
}
return array(val, options);
},
validate: validateUniqueArrayFieldSelect(
"options",
"value",
sortOptions,
"charterafrica.site:uniqueSortOptions",
),
},
{
name: "label",
Expand Down
27 changes: 9 additions & 18 deletions apps/charterafrica/src/payload/fields/documentCloudFilterBar.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { deepmerge } from "@mui/utils";
import { array } from "payload/dist/fields/validations";

import defaultValue from "../utils/defaultValues";
import validateUniqueArrayFieldSelect from "../utils/validateUniqueArrayFieldSelect";

import filterBar from "./filterBar";

const documentSortOptions = [
"created_at",
"score",
"title",
"page_count",
"source",
];
const sortOptions = ["created_at", "score", "title", "page_count", "source"];

function documentCloudFilterBar({ overrides } = {}) {
const generatedDocumentCloudFilterBar = filterBar({
Expand Down Expand Up @@ -81,16 +75,13 @@ function documentCloudFilterBar({ overrides } = {}) {
name: "value",
type: "select",
required: true,
options: documentSortOptions,
validate: (val, options) => {
const { data, t } = options || {};
if (
data?.options?.filter((l) => l.value === val)?.length > 1
) {
return t("charterafrica.site:uniqueSortOptions");
}
return array(val, options);
},
options: sortOptions,
validate: validateUniqueArrayFieldSelect(
"options",
"value",
sortOptions,
"charterafrica.site:uniqueSortOptions",
),
},
{
name: "label",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { select } from "payload/dist/fields/validations";

function validateUniqueArrayFieldSelect(
arrayField,
selectField,
selectOptions,
validationResourceMessage,
overrides = {},
) {
const { name: arrayFieldName = arrayField } = overrides;

return function validate(val, args) {
const { data, name: selectFieldName = selectField, t } = args || {};
if (
data?.[arrayFieldName]?.filter((l) => l?.[selectFieldName] === val)
?.length > 1
) {
return t(validationResourceMessage);
}

const { hasMany, options = selectOptions, required = true } = args;
return select(val, { hasMany, options, required, t });
};
}

export default validateUniqueArrayFieldSelect;

0 comments on commit d62e47d

Please sign in to comment.