Skip to content

Commit

Permalink
feat(endpoint-posts): use validation schema from post type config
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Jan 24, 2024
1 parent 650791b commit aa348c8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 90 deletions.
4 changes: 2 additions & 2 deletions packages/endpoint-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export default class PostsEndpoint {
router.post("/new", newController.post);

router.get("/create", postData.create, formController.get);
router.post("/create", postData.create, validate, formController.post);
router.post("/create", postData.create, validate(), formController.post);

router.use("/:uid/:action?", postData.read);
router.get("/:uid", postController);

router.get("/:uid/update", formController.get);
router.post("/:uid/update", validate, formController.post);
router.post("/:uid/update", validate(), formController.post);

router.get("/:uid/:action(delete|undelete)", deleteController.get);
router.post("/:uid/:action(delete|undelete)", deleteController.post);
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint-posts/lib/controllers/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const formController = {
// @todo Use `properties` for field names whose values should be submitted
delete values["all-day"];
delete values.geo;
delete values["post-type"];
delete values.postType;
delete values["publication-date"];

const mf2 = jf2ToMf2({ properties: sanitise(values) });
Expand Down
109 changes: 23 additions & 86 deletions packages/endpoint-posts/lib/middleware/validation.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,24 @@
import { check } from "express-validator";
import { ISO_6709_RE } from "@indiekit/util";
import { checkSchema } from "express-validator";

export const validate = [
check("audio.*")
.if((value, { req }) => req.body?.["post-type"] === "audio")
.notEmpty()
.withMessage((value, { req }) =>
req.__(`posts.error.media.empty`, "/music/audio.mp3"),
),
check("bookmark-of")
.if((value, { req }) => req.body?.["post-type"] === "bookmark")
.exists()
.isURL()
.withMessage((value, { req }) =>
req.__(`posts.error.url.empty`, "https://example.org"),
),
check("in-reply-to")
.if(
(value, { req }) =>
req.body?.["post-type"] === "reply" ||
req.body?.["post-type"] === "rsvp",
)
.exists()
.isURL()
.withMessage((value, { req }) =>
req.__(`posts.error.url.empty`, "https://example.org"),
),
check("like-of")
.if((value, { req }) => req.body?.["post-type"] === "like")
.exists()
.isURL()
.withMessage((value, { req }) =>
req.__(`posts.error.url.empty`, "https://example.org"),
),
check("repost-of")
.if((value, { req }) => req.body?.["post-type"] === "repost")
.exists()
.isURL()
.withMessage((value, { req }) =>
req.__(`posts.error.url.empty`, "https://example.org"),
),
check("name")
.if(
(value, { req }) =>
req.body?.["post-type"] === "article" ||
req.body?.["post-type"] === "bookmark" ||
req.body?.["post-type"] === "event",
)
.notEmpty()
.withMessage((value, { req, path }) => req.__(`posts.error.${path}.empty`)),
check("start")
.if((value, { req }) => req.body?.["post-type"] === "event")
.notEmpty()
.withMessage((value, { req, path }) => req.__(`posts.error.${path}.empty`)),
check("content")
.if(
(value, { req }) =>
req.body?.["post-type"] === "article" ||
req.body?.["post-type"] === "note" ||
req.body?.["post-type"] === "reply",
)
.notEmpty()
.withMessage((value, { req, path }) => req.__(`posts.error.${path}.empty`)),
check("photo.*.url")
.if((value, { req }) => req.body?.["post-type"] === "photo")
.notEmpty()
.withMessage((value, { req }) =>
req.__(`posts.error.media.empty`, "/photos/image.jpg"),
),
check("photo.*.alt")
.if((value, { req }) => req.body?.["post-type"] === "photo")
.notEmpty()
.withMessage((value, { req }) => req.__(`posts.error.mp-photo-alt.empty`)),
check("geo")
.if((value, { req }) => req.body?.geo)
.custom((value) => value.match(ISO_6709_RE))
.withMessage((value, { req, path }) =>
req.__(`posts.error.${path}.invalid`),
),
check("video.*")
.if((value, { req }) => req.body?.["post-type"] === "video")
.notEmpty()
.withMessage((value, { req }) =>
req.__(`posts.error.media.empty`, "/movies/video.mp4"),
),
];
export const validate = () => {
return async (request, response, next) => {
const { postTypes } = request.app.locals.publication;
const validations = [];

for (const postType of postTypes) {
if (!postType.validationSchema) {
continue;
}

for (const schema of postType.validationSchema) {
validations.push(checkSchema(schema));
}
}

for (let validation of validations) {
await validation.run(request);
}

next();
};
};
2 changes: 1 addition & 1 deletion packages/endpoint-posts/views/post-form.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}) | indent(2) }}

{{ input({
name: "post-type",
name: "postType",
type: "hidden",
value: postType
}) | indent(2) }}
Expand Down

0 comments on commit aa348c8

Please sign in to comment.