Use AJV, refactor a lot
This commit is contained in:
10
api/models/schemas.js
Normal file
10
api/models/schemas.js
Normal file
@@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
"email": {
|
||||
"type": "string",
|
||||
"format": "email"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
}
|
||||
@@ -2,30 +2,23 @@
|
||||
/**
|
||||
* Model: Users
|
||||
*/
|
||||
|
||||
const utils = require(`../utils`);
|
||||
const db = require(`./db`);
|
||||
const utils = require(`../../utils`);
|
||||
const db = require(`../db`);
|
||||
const schemas = require(`./schemas`);
|
||||
const sharedSchemas = require(`../schemas`);
|
||||
const validation = require(`../validation`);
|
||||
|
||||
/**
|
||||
* Register user
|
||||
* Create a new user
|
||||
* @param {string} user.email User email
|
||||
* @param {string} user.password User password
|
||||
*/
|
||||
const register = async (user = {}) => {
|
||||
if (!user.email) {
|
||||
throw new Error(`"email" is required`);
|
||||
}
|
||||
if (!user.password) {
|
||||
throw new Error(`"password" is required`);
|
||||
}
|
||||
if (!utils.validateEmailAddress(user.email)) {
|
||||
throw new Error(`"${user.email}" is not a valid email address`);
|
||||
}
|
||||
const create = async (user = {}) => {
|
||||
validation.validateOrThrow(schemas.create, user);
|
||||
|
||||
// Check if user is already registered
|
||||
const existingUser = await getByEmail(user.email);
|
||||
if (existingUser) {
|
||||
throw new Error(
|
||||
throw utils.customError(
|
||||
`A user with email "${user.email}" is already registered`
|
||||
);
|
||||
}
|
||||
@@ -40,16 +33,10 @@ const register = async (user = {}) => {
|
||||
|
||||
/**
|
||||
* Get user by email address
|
||||
* @param {string} email
|
||||
* @param {string} email Email address of user to retrieve
|
||||
*/
|
||||
|
||||
const getByEmail = async (email) => {
|
||||
if (!email) {
|
||||
throw new Error(`"email" is required`);
|
||||
}
|
||||
if (!utils.validateEmailAddress(email)) {
|
||||
throw new Error(`"${email}" is not a valid email address`);
|
||||
}
|
||||
validation.validateOrThrow(sharedSchemas.email, email);
|
||||
|
||||
let user = await db.getByKey(email);
|
||||
|
||||
@@ -61,11 +48,8 @@ const getByEmail = async (email) => {
|
||||
* Get user by id
|
||||
* @param {string} id
|
||||
*/
|
||||
|
||||
const getById = async (id) => {
|
||||
if (!id) {
|
||||
throw new Error(`"id" is required`);
|
||||
}
|
||||
validation.validateOrThrow(sharedSchemas.id, id);
|
||||
|
||||
let user = await db.getById(`user`, id);
|
||||
|
||||
@@ -97,7 +81,7 @@ const convertToPublicFormat = (user = {}) => {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
create,
|
||||
getByEmail,
|
||||
getById,
|
||||
convertToPublicFormat,
|
||||
19
api/models/users/schemas.js
Normal file
19
api/models/users/schemas.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const sharedSchemas = require(`../schemas`);
|
||||
|
||||
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"]
|
||||
}
|
||||
};
|
||||
16
api/models/validation.js
Normal file
16
api/models/validation.js
Normal file
@@ -0,0 +1,16 @@
|
||||
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,
|
||||
};
|
||||
Reference in New Issue
Block a user