From e3ff1f262509f64f3eac742b36819d7d09b3c4bd Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Thu, 11 Feb 2021 21:44:49 -0800 Subject: [PATCH] project name validation --- frontend/nuxt.config.ts | 2 +- frontend/pages/new.vue | 32 +++++++++++++++++-- frontend/plugins/api.ts | 4 --- frontend/plugins/utils.ts | 2 +- .../internal/ProjectController.java | 4 +-- .../internal/projects/ProjectFactory.java | 8 ++--- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/frontend/nuxt.config.ts b/frontend/nuxt.config.ts index 7cd17b10..ef1e5409 100644 --- a/frontend/nuxt.config.ts +++ b/frontend/nuxt.config.ts @@ -29,7 +29,7 @@ export default { css: ['~/assets/main.scss'], // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins - plugins: ['~/plugins/utils.ts', '~/plugins/api.ts', '~/plugins/auth.ts'], + plugins: ['~/plugins/api.ts', '~/plugins/utils.ts', '~/plugins/auth.ts'], // Auto import components: https://go.nuxtjs.dev/config-components components: false, // Can change this back if you really want, but it doesn't look like Webstorm or Intellij understand what's going on if they aren't imported. Also, does it really matter? It's just a few imports diff --git a/frontend/pages/new.vue b/frontend/pages/new.vue index bbef56e3..308f111f 100644 --- a/frontend/pages/new.vue +++ b/frontend/pages/new.vue @@ -44,7 +44,7 @@ item-text="name" item-value="userId" :label="$t('project.new.step2.userselect')" - :rules="[$util.$vc.require()]" + :rules="[$util.$vc.require('Project owner')]" :append-icon="createAsIcon" /> @@ -55,6 +55,7 @@ autofocus dense filled + :error-messages="nameErrors" :label="$t('project.new.step2.projectname')" :rules="[$util.$vc.require('Name')]" append-icon="mdi-form-textbox" @@ -234,9 +235,10 @@ diff --git a/frontend/plugins/api.ts b/frontend/plugins/api.ts index d262f0c1..b4215919 100644 --- a/frontend/plugins/api.ts +++ b/frontend/plugins/api.ts @@ -86,10 +86,6 @@ const createApi = ({ $axios, store, app: { $cookies } }: Context) => { private _request(url: string, token: string | null, method: AxiosRequestConfig['method'], data: object): Promise { const headers: Record = token ? { Authorization: `HangarAuth ${token}` } : {}; - // if (data instanceof FormData) { - // headers['Content-Type'] = 'multipart/form-data'; - // } - return new Promise((resolve, reject) => { return $axios .request({ diff --git a/frontend/plugins/utils.ts b/frontend/plugins/utils.ts index d6439be0..562a7f86 100644 --- a/frontend/plugins/utils.ts +++ b/frontend/plugins/utils.ts @@ -14,7 +14,7 @@ import { NotifPayload } from '~/store/snackbar'; import { AuthState } from '~/store/auth'; type Validation = (v: string) => boolean | string; -type ValidationArgument = (field?: string | TranslateResult) => Validation; +type ValidationArgument = (any: any) => Validation; function handleRequestError(err: AxiosError, error: Context['error']) { if (!err.isAxiosError) { diff --git a/src/main/java/io/papermc/hangar/controller/internal/ProjectController.java b/src/main/java/io/papermc/hangar/controller/internal/ProjectController.java index 65cd8bf5..e382023e 100644 --- a/src/main/java/io/papermc/hangar/controller/internal/ProjectController.java +++ b/src/main/java/io/papermc/hangar/controller/internal/ProjectController.java @@ -51,8 +51,8 @@ public class ProjectController extends HangarController { @GetMapping("/validateName") @ResponseStatus(HttpStatus.OK) - public void validateProjectName(@RequestParam long userId, @RequestParam String projectName) { - projectFactory.checkProjectAvailability(userId, projectName); + public void validateProjectName(@RequestParam long userId, @RequestParam String value) { + projectFactory.checkProjectAvailability(userId, value); } @GetMapping("/possibleOwners") diff --git a/src/main/java/io/papermc/hangar/service/internal/projects/ProjectFactory.java b/src/main/java/io/papermc/hangar/service/internal/projects/ProjectFactory.java index 62003299..089a545b 100644 --- a/src/main/java/io/papermc/hangar/service/internal/projects/ProjectFactory.java +++ b/src/main/java/io/papermc/hangar/service/internal/projects/ProjectFactory.java @@ -73,16 +73,16 @@ public class ProjectFactory extends HangarService { invalidProjectReason = projectsDAO.checkProjectValidity(userId, name, StringUtils.slugify(name)); } if (invalidProjectReason != null) { - throw new HangarApiException(HttpStatus.BAD_REQUEST, invalidProjectReason.key); + throw new HangarApiException(HttpStatus.CONFLICT, invalidProjectReason.key); } } @EnumByName public enum InvalidProjectReason { - OWNER_NAME("error.project.nameExists"), - OWNER_SLUG("error.project.slugExists"), - INVALID_NAME("error.project.invalidName"); + OWNER_NAME("project.new.error.nameExists"), + OWNER_SLUG("project.new.error.slugExists"), + INVALID_NAME("project.new.error.invalidName"); private final String key;