-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): add
unique()
validation
- Loading branch information
Showing
8 changed files
with
122 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Is } from '@athenna/common' | ||
import { Database } from '@athenna/database' | ||
import { ServiceProvider } from '@athenna/ioc' | ||
import type { FieldContext } from '@vinejs/vine/types' | ||
import vine, { SimpleErrorReporter, VineString } from '@vinejs/vine' | ||
import { ValidationException } from '#src/exceptions/validation.exception' | ||
|
||
type UniqueOptions = { | ||
table: string | ||
column?: string | ||
} | ||
|
||
declare module '@vinejs/vine' { | ||
interface VineString { | ||
unique(options: UniqueOptions): this | ||
} | ||
} | ||
|
||
export class ErrorReporter extends SimpleErrorReporter { | ||
createError(): any { | ||
return new ValidationException(this.errors) | ||
} | ||
} | ||
|
||
export default class ValidatorProvider extends ServiceProvider { | ||
public async boot() { | ||
vine.errorReporter = () => new ErrorReporter() | ||
|
||
const uniqueRule = vine.createRule(this.unique) | ||
|
||
VineString.macro( | ||
'unique', | ||
function (this: VineString, options: UniqueOptions) { | ||
return this.use(uniqueRule(options)) | ||
} | ||
) | ||
} | ||
|
||
public async unique( | ||
value: unknown, | ||
options: UniqueOptions, | ||
field: FieldContext | ||
) { | ||
/** | ||
* We do not want to deal with non-string | ||
* values. The "string" rule will handle the | ||
* the validation. | ||
*/ | ||
if (!Is.String(value)) { | ||
return | ||
} | ||
|
||
if (!options.column) { | ||
options.column = field.name as string | ||
} | ||
|
||
const existsRow = await Database.table(options.table) | ||
.select(options.column) | ||
.where(options.column, value) | ||
.exists() | ||
|
||
if (existsRow) { | ||
field.report('The {{ field }} field is not unique', 'unique', field) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,13 @@ | ||
import type { Context } from '@athenna/http' | ||
import vine, { type Vine } from '@vinejs/vine' | ||
import type { SchemaTypes } from '@vinejs/vine/types' | ||
import { ValidationException } from '#src/exceptions/validation.exception' | ||
|
||
export abstract class BaseValidator { | ||
public abstract definition: SchemaTypes | ||
public abstract handleHttp(ctx: Context): Promise<void> | ||
protected validator: Vine = vine | ||
|
||
protected schema: Vine = vine | ||
public abstract schema: SchemaTypes | ||
public abstract handle(data: any): Promise<void> | ||
|
||
public handle(ctx: Context): Promise<void> { | ||
return this.handleHttp(ctx) | ||
} | ||
|
||
protected async validate(data: any) { | ||
try { | ||
await vine.validate({ schema: this.definition, data }) | ||
} catch (err) { | ||
throw new ValidationException(err) | ||
} | ||
public validate(data: any) { | ||
return this.validator.validate({ schema: this.schema, data }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
{"default":[],"deadletter":[],"user:email":[],"user:password":[{"user":{"name":"João Lenon","email":"[email protected]"},"password":"$2b$10$wpmwbiGnqA.W0toBLwGQsu9li3/qldRGx6vwGReQYx1PmQd6bkmMC"}],"user:email:password":[],"user:confirm":[]} | ||
{ | ||
"default": [], | ||
"deadletter": [], | ||
"user:email": [], | ||
"user:password": [], | ||
"user:email:password": [], | ||
"user:confirm": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ export default class AuthControllerTest extends BaseHttpTest { | |
|
||
@Test() | ||
public async shouldThrowUnauthorizedExceptionIfAuthenticatedDontHaveRolesKey({ request }: Context) { | ||
const token = await jwt.sign({ user: { id: -1 } }, Config.get('auth.jwt.secret'), { | ||
const token = jwt.sign({ user: { id: -1 } }, Config.get('auth.jwt.secret'), { | ||
expiresIn: Config.get('auth.jwt.expiresIn') | ||
}) | ||
|
||
|
@@ -55,7 +55,7 @@ export default class AuthControllerTest extends BaseHttpTest { | |
|
||
@Test() | ||
public async shouldThrowUnauthorizedExceptionIfAuthenticatedUserCannotBeFound({ request }: Context) { | ||
const token = await jwt.sign({ user: { id: -1, roles: [] } }, Config.get('auth.jwt.secret'), { | ||
const token = jwt.sign({ user: { id: -1, roles: [] } }, Config.get('auth.jwt.secret'), { | ||
expiresIn: Config.get('auth.jwt.expiresIn') | ||
}) | ||
|
||
|
@@ -238,6 +238,33 @@ export default class AuthControllerTest extends BaseHttpTest { | |
}) | ||
} | ||
|
||
@Test() | ||
public async shouldThrowValidationErrorWhenTryingToCreateAnUserWithAnEmailThatAlreadyExists({ request }: Context) { | ||
const response = await request.post('/api/v1/register', { | ||
body: { | ||
name: 'Test', | ||
email: '[email protected]', | ||
password: '12345678' | ||
} | ||
}) | ||
|
||
response.assertStatusCode(422) | ||
response.assertBodyContains({ | ||
data: { | ||
code: 'E_VALIDATION_ERROR', | ||
message: 'Validation failure', | ||
name: 'ValidationException', | ||
details: [ | ||
{ | ||
field: 'email', | ||
message: 'The email field is not unique', | ||
rule: 'unique' | ||
} | ||
] | ||
} | ||
}) | ||
} | ||
|
||
@Test() | ||
public async shouldBeAbleToConfirmUserAccount({ assert, request }: Context) { | ||
const user = await User.factory().create({ token: Uuid.generate(), emailVerifiedAt: null }) | ||
|