Browse Source

adding

master
HerrHase 3 years ago
parent
commit
fb7f4297e3
3 changed files with 14 additions and 7 deletions
  1. +3
    -2
      src/http/api/users.ts
  2. +2
    -1
      src/middleware/users.ts
  3. +9
    -4
      src/rules/uniqueUser.ts

+ 3
- 2
src/http/api/users.ts View File

@ -1,5 +1,6 @@
import { v4 } from "https://deno.land/std@0.99.0/uuid/mod.ts"; 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 { 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' 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 let user
const [ valid, errors ] = await validate(body, { const [ valid, errors ] = await validate(body, {
email: [ isEmail, required ],
email: [ isEmail, required, uniqueUser('email') ],
password: [ maxLength(64) ], password: [ maxLength(64) ],
displayname: [ maxLength(128) ] displayname: [ maxLength(128) ]
}) })
@ -97,7 +98,7 @@ router.put('/:id', async function(request, response, next)
let user let user
const [ valid, errors ] = await validate(body, { const [ valid, errors ] = await validate(body, {
email: [ isEmail, required ],
email: [ isEmail, required, uniqueUser('email') ],
password: [ maxLength(64) ], password: [ maxLength(64) ],
displayname: [ maxLength(128) ] displayname: [ maxLength(128) ]
}) })


+ 2
- 1
src/middleware/users.ts View File

@ -7,12 +7,13 @@ import UserRepository from '../repositories/user.ts'
const router = Router() const router = Router()
/** /**
* check every route for single bucket
* check route for user if parameter is
* *
* @param request * @param request
* @param response * @param response
* @param next * @param next
* @return * @return
*
*/ */
async function usersMiddleware(request: any, response: any, next: any) async function usersMiddleware(request: any, response: any, next: any)
{ {


src/rules/unique.ts → src/rules/uniqueUser.ts View File

@ -1,4 +1,6 @@
import { invalid, Validity, Rule } from "https://deno.land/x/validasaur/mod.ts"; 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 * search for key
@ -7,19 +9,22 @@ import { invalid, Validity, Rule } from "https://deno.land/x/validasaur/mod.ts";
* @param value * @param value
* @return * @return
*/ */
export function unique(key: string, value: string): Rule {
export function uniqueUser(key: string): Rule {
return async function uniqueRule(value: any): Promise<Validity> { return async function uniqueRule(value: any): Promise<Validity> {
console.log(value)
if (typeof value !== 'string' && typeof value !== 'number') { if (typeof value !== 'string' && typeof value !== 'number') {
return invalid('unique', { value, table, column });
return invalid('unique', { key, value });
} }
const db = new Database<UserSchema>('./storage/database/users.json')
const data = await db.findOne({ const data = await db.findOne({
key: value
[ key ]: value
}) })
if (data !== null) { if (data !== null) {
return invalid('unique', { value, table, column });
return invalid('unique', { key, value });
} }
} }
} }

Loading…
Cancel
Save