-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from medishen/dev/v2
Refactor Validation System and Enhance Testing for Improved Flexibility and Reliability
- Loading branch information
Showing
30 changed files
with
632 additions
and
194 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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,9 +1,9 @@ | ||
export * from './Cache'; | ||
export * from './Controller'; | ||
export * from './Guards'; | ||
export * from './MultiLang'; | ||
export * from './Transform'; | ||
export * from './http'; | ||
export * from './Cache.decorator'; | ||
export * from './Controller.decorator'; | ||
export * from './Guards.decorator'; | ||
export * from './MultiLang.decorator'; | ||
export * from './Transform.decorator'; | ||
export * from './http.decorator'; | ||
export * from './module'; | ||
export * from './Middleware'; | ||
export * from './Validator'; | ||
export * from './Middleware.decorator'; | ||
export * from './Validator.decorator'; |
File renamed without changes.
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,2 +1,2 @@ | ||
export * from './Injector'; | ||
export * from './Module'; | ||
export * from './Module.decorator'; |
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,9 +1,12 @@ | ||
export class ValidationError { | ||
errors: Record<string, Record<string, string[]>> = {}; | ||
|
||
addError(section: string, field: string, message: string[]): void { | ||
if (!this.errors[section]) this.errors[section] = {}; | ||
if (!this.errors[section][field]) this.errors[section][field] = []; | ||
this.errors[section][field].push(...message); | ||
} | ||
/** Check if there are any errors */ | ||
hasErrors(): boolean { | ||
return Object.keys(this.errors).length > 0; | ||
} | ||
} |
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,96 +1 @@ | ||
import { NestedSchema, Rule, Schema } from '../decorator'; | ||
import { ValidatorProcess } from './Validator'; | ||
@Schema({ | ||
section: 'headers', | ||
defaultRules: ['required'], | ||
}) | ||
export class HeaderSchema { | ||
@Rule() | ||
example_header = ['boolean']; | ||
} | ||
|
||
@Schema({ | ||
section: 'body', | ||
defaultRules: ['required'], | ||
}) | ||
export class BodySchema { | ||
@Rule({ | ||
messages: { | ||
required: 'The username is required.', | ||
string: 'The username must be a string.', | ||
min: 'The username must be at least 3 characters long.', | ||
}, | ||
}) | ||
username = ['string', 'min:3']; | ||
|
||
@Rule({ | ||
messages: { | ||
required: 'The email is required.', | ||
email: 'The email must be a valid email address.', | ||
}, | ||
}) | ||
email = ['string', 'email']; | ||
|
||
@Rule({ | ||
messages: { | ||
required: 'The password is required.', | ||
string: 'The password must be a string.', | ||
min: 'The password must be at least 8 characters long.', | ||
}, | ||
}) | ||
password = ['min:8']; | ||
@Rule({ | ||
messages: { | ||
required: 'The "isActive" field is required.', | ||
boolean: 'The "isActive" field must be a boolean.', | ||
}, | ||
}) | ||
isActive = ['boolean']; | ||
@Rule({ | ||
messages: { | ||
string: 'The "notes" field must be a string.', | ||
}, | ||
}) | ||
notes? = ['string']; | ||
} | ||
|
||
export class UserSchema { | ||
@NestedSchema({ | ||
conditions: [ | ||
{ | ||
message: '', | ||
schema: HeaderSchema, | ||
}, | ||
], | ||
}) | ||
body = BodySchema; | ||
@NestedSchema() | ||
headers = HeaderSchema; | ||
} | ||
const userSchame = new UserSchema(); | ||
// const username = userSchame.body.username; /** I want username errors to be returned when the user says this. What should I do? */ | ||
// console.log('username:', username); // like this {"The username is required."} | ||
|
||
const example3: { | ||
body: any; | ||
headers: any; | ||
} = { | ||
body: { | ||
username: 'admin', | ||
password: 'Admin123', | ||
email: 'invalid', | ||
isActive: true, | ||
notes: 'Notes about the admin.', | ||
}, | ||
headers: { | ||
example_header: 'hello', | ||
}, | ||
}; | ||
|
||
// Validation Process | ||
(async () => { | ||
console.log('=== Validation Errors: Example 3 ==='); | ||
const result = await ValidatorProcess.validate(UserSchema, example3); | ||
|
||
console.log(JSON.stringify(result, null, 2)); | ||
})(); | ||
export * from './Validator'; |
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
Oops, something went wrong.