From a1d664a164e421137c654c832cdb80a47150431c Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Mon, 16 Aug 2021 13:50:18 +0100 Subject: [PATCH] refactor: remove unused schemas Refs #397 --- src/common/schemas/group.js | 16 ------------ src/common/schemas/preprint.js | 46 --------------------------------- src/common/schemas/prereview.js | 44 ------------------------------- src/common/schemas/setting.js | 15 ----------- src/common/schemas/user.js | 38 --------------------------- 5 files changed, 159 deletions(-) delete mode 100644 src/common/schemas/group.js delete mode 100644 src/common/schemas/preprint.js delete mode 100644 src/common/schemas/prereview.js delete mode 100644 src/common/schemas/setting.js delete mode 100644 src/common/schemas/user.js diff --git a/src/common/schemas/group.js b/src/common/schemas/group.js deleted file mode 100644 index 3c8dd9e4..00000000 --- a/src/common/schemas/group.js +++ /dev/null @@ -1,16 +0,0 @@ -import { UnprocessableError } from '../../common/errors.js'; -import Joi from '@hapi/joi'; - -// TODO: includes ndt5 & ndt7 fields, add from DASH & speedtest-cli after feedback -const schema = Joi.object({ - name: Joi.string(), -}); - -export async function validate(data) { - try { - const value = await schema.validateAsync(data); - return value; - } catch (err) { - throw new UnprocessableError('Unable to validate test JSON: ', err); - } -} diff --git a/src/common/schemas/preprint.js b/src/common/schemas/preprint.js deleted file mode 100644 index 0b5e0e02..00000000 --- a/src/common/schemas/preprint.js +++ /dev/null @@ -1,46 +0,0 @@ -import { UnprocessableError } from '../../common/errors.js'; -import Joi from 'joi'; - -const creationSchema = Joi.array() - .items( - Joi.object({ - doi: Joi.string(), - title: Joi.string(), - server: Joi.string(), - url: Joi.string(), - pdfUrl: Joi.string(), - }), - ) - .min(1); - -const updateSchema = Joi.array() - .items( - Joi.object({ - doi: Joi.string(), - title: Joi.string(), - server: Joi.string(), - url: Joi.string(), - pdfUrl: Joi.string(), - }), - ) - .min(1); - -export async function validateCreation(data) { - try { - data = Array.isArray(data) ? data : [data]; - const value = await creationSchema.validateAsync(data); - return value; - } catch (err) { - throw new UnprocessableError('Unable to validate JSON: ', err); - } -} - -export async function validateUpdate(data) { - try { - data = Array.isArray(data) ? data : [data]; - const value = await updateSchema.validateAsync(data); - return value; - } catch (err) { - throw new UnprocessableError('Unable to validate JSON: ', err); - } -} diff --git a/src/common/schemas/prereview.js b/src/common/schemas/prereview.js deleted file mode 100644 index 23d95313..00000000 --- a/src/common/schemas/prereview.js +++ /dev/null @@ -1,44 +0,0 @@ -import { UnprocessableError } from '../../common/errors.js'; -import Joi from '@hapi/joi'; - -const creationSchema = Joi.array() - .items( - Joi.object({ - doi: Joi.string(), - authors: Joi.array(), - is_hidden: Joi.boolean(), - content: Joi.array(), - }), - ) - .min(1); - -const updateSchema = Joi.array() - .items( - Joi.object({ - doi: Joi.string().required(), - authors: Joi.array(), - is_hidden: Joi.boolean().required(), - content: Joi.array().min(1), - }), - ) - .min(1); - -export async function validateCreation(data) { - try { - data = Array.isArray(data) ? data : [data]; - const value = await creationSchema.validateAsync(data); - return value; - } catch (err) { - throw new UnprocessableError('Unable to validate JSON: ', err); - } -} - -export async function validateUpdate(data) { - try { - data = Array.isArray(data) ? data : [data]; - const value = await updateSchema.validateAsync(data); - return value; - } catch (err) { - throw new UnprocessableError('Unable to validate JSON: ', err); - } -} diff --git a/src/common/schemas/setting.js b/src/common/schemas/setting.js deleted file mode 100644 index 572e488d..00000000 --- a/src/common/schemas/setting.js +++ /dev/null @@ -1,15 +0,0 @@ -import { UnprocessableError } from '../../common/errors.js'; -import Joi from '@hapi/joi'; - -const schema = Joi.object({ - value: Joi.string(), -}); - -export async function validate(data) { - try { - const value = await schema.validateAsync(data); - return value; - } catch (err) { - throw new UnprocessableError('Unable to validate test JSON: ', err); - } -} diff --git a/src/common/schemas/user.js b/src/common/schemas/user.js deleted file mode 100644 index 6cb37f73..00000000 --- a/src/common/schemas/user.js +++ /dev/null @@ -1,38 +0,0 @@ -import { UnprocessableError } from '../../common/errors.js'; -import Joi from '@hapi/joi'; - -const schema = Joi.object({ - username: Joi.string(), - password: Joi.string(), - id: Joi.number(), - firstName: Joi.string(), - lastName: Joi.string(), - email: Joi.string(), - role: Joi.number(), -}); - -// schema for users editing their own account -const userSchema = Joi.object({ - username: Joi.string(), - oldPassword: Joi.string().allow(''), - newPassword: Joi.string().allow(''), - id: Joi.number(), - firstName: Joi.string(), - lastName: Joi.string(), - email: Joi.string(), - role: Joi.number(), -}); - -export async function validate(data, user = false) { - try { - let value; - if (user) { - value = await userSchema.validateAsync(data); - } else { - value = await schema.validateAsync(data); - } - return value; - } catch (err) { - throw new UnprocessableError('Unable to validate test JSON: ', err); - } -}