Format and test fixes

This commit is contained in:
2020-11-18 21:17:29 -06:00
parent 95d7360ee4
commit 172e208edd
8 changed files with 55 additions and 27 deletions

View File

@@ -1,10 +0,0 @@
module.exports = {
"email": {
"type": "string",
"format": "email"
},
"id": {
"type": "string",
"minLength": 1
}
}

View File

@@ -1,12 +1,11 @@
"use strict";
/**
* Model: Users
*/
const utils = require(`../../utils`);
const db = require(`../db`);
const schemas = require(`./schemas`);
const sharedSchemas = require(`../schemas`);
const validation = require(`../validation`);
const commonSchemas = require(`../../validation/commonSchemas`);
const validation = require(`../../validation`);
/**
* Create a new user
@@ -19,7 +18,8 @@ const create = async (user = {}) => {
const existingUser = await getByEmail(user.email);
if (existingUser) {
throw utils.customError(
`A user with email "${user.email}" is already registered`
`A user with email "${user.email}" is already registered`,
400
);
}
@@ -36,7 +36,7 @@ const create = async (user = {}) => {
* @param {string} email Email address of user to retrieve
*/
const getByEmail = async (email) => {
validation.validateOrThrow(sharedSchemas.email, email);
validation.validateOrThrow(commonSchemas.email, email);
let user = await db.getByKey(email);
@@ -49,7 +49,7 @@ const getByEmail = async (email) => {
* @param {string} id
*/
const getById = async (id) => {
validation.validateOrThrow(sharedSchemas.id, id);
validation.validateOrThrow(commonSchemas.id, id);
let user = await db.getById(`user`, id);

View File

@@ -1,19 +1,21 @@
const sharedSchemas = require(`../schemas`);
const commonSchemas = require(`../../validation/commonSchemas`);
const create = {
$schema: "http://json-schema.org/draft-07/schema#",
title: "Create User",
description: "An object to create a new user",
type: "object",
properties: {
email: commonSchemas.email,
password: {
type: "string",
minLength: 10,
maxLength: 50,
},
},
required: ["email", "password"],
};
module.exports = {
"create": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Create User",
"description": "An object to create a new user",
"type": "object",
"properties": {
"email": sharedSchemas.email,
"password": {
"type": "string",
"minLength": 10,
"maxLength": 50
}
},
"required": ["email", "password"]
}
create,
};

View File

@@ -1,16 +0,0 @@
const Ajv = require(`ajv`);
const { validationError } = require(`../utils`);
const validateOrThrow = (schema, data) => {
const ajv = new Ajv();
const valid = ajv.validate(schema, data);
if (!valid) {
throw validationError(ajv.errors);
}
};
module.exports = {
validateOrThrow,
};