diff --git a/src/http/api/users.ts b/src/http/api/users.ts index d0ee128..11360ad 100644 --- a/src/http/api/users.ts +++ b/src/http/api/users.ts @@ -1,5 +1,6 @@ import { v4 } from "https://deno.land/std@0.99.0/uuid/mod.ts"; import { validate, required, maxLength, isEmail } from 'https://deno.land/x/validasaur@v0.15.0/mod.ts' +import { uniqueUser } from '../../rules/uniqueUser.ts' import { Router } from 'https://deno.land/x/opine@1.5.3/mod.ts' @@ -62,7 +63,7 @@ router.post('/', async function(request, response) let user const [ valid, errors ] = await validate(body, { - email: [ isEmail, required ], + email: [ isEmail, required, uniqueUser('email') ], password: [ maxLength(64) ], displayname: [ maxLength(128) ] }) @@ -97,7 +98,7 @@ router.put('/:id', async function(request, response, next) let user const [ valid, errors ] = await validate(body, { - email: [ isEmail, required ], + email: [ isEmail, required, uniqueUser('email') ], password: [ maxLength(64) ], displayname: [ maxLength(128) ] }) diff --git a/src/middleware/users.ts b/src/middleware/users.ts index 01ac836..97bfb4f 100644 --- a/src/middleware/users.ts +++ b/src/middleware/users.ts @@ -7,12 +7,13 @@ import UserRepository from '../repositories/user.ts' const router = Router() /** - * check every route for single bucket + * check route for user if parameter is * * @param request * @param response * @param next * @return + * */ async function usersMiddleware(request: any, response: any, next: any) { diff --git a/src/rules/unique.ts b/src/rules/uniqueUser.ts similarity index 50% rename from src/rules/unique.ts rename to src/rules/uniqueUser.ts index 05ce4ca..a2b3861 100644 --- a/src/rules/unique.ts +++ b/src/rules/uniqueUser.ts @@ -1,4 +1,6 @@ import { invalid, Validity, Rule } from "https://deno.land/x/validasaur/mod.ts"; +import { Database } from 'https://deno.land/x/aloedb@0.9.0/mod.ts' +import { UserSchema } from './../stores/user.ts' /** * search for key @@ -7,19 +9,22 @@ import { invalid, Validity, Rule } from "https://deno.land/x/validasaur/mod.ts"; * @param value * @return */ -export function unique(key: string, value: string): Rule { +export function uniqueUser(key: string): Rule { return async function uniqueRule(value: any): Promise { + console.log(value) + if (typeof value !== 'string' && typeof value !== 'number') { - return invalid('unique', { value, table, column }); + return invalid('unique', { key, value }); } + const db = new Database('./storage/database/users.json') const data = await db.findOne({ - key: value + [ key ]: value }) if (data !== null) { - return invalid('unique', { value, table, column }); + return invalid('unique', { key, value }); } } } \ No newline at end of file