-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,156 additions
and
987 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Moving From 1.x to 2.x | ||
|
||
## SchemaBase has been removed | ||
|
||
The `SchemaBase` class has been removed. This class was overriding the constructor and may cause problems in the future. | ||
|
||
The user is now responsible to handle property assignment to the class manually. | ||
|
||
Here's a simple function replacing the SchemaBase's behavior. | ||
|
||
```ts | ||
function validateFromPayload(validationClass: any, payload: Record<string, any>) { | ||
const validationClassInstance = new validationClass(); | ||
Object.assign(validationClassInstance,payload); | ||
return validate(validationClassInstance); | ||
} | ||
``` | ||
|
||
### Before | ||
|
||
```ts | ||
@Schema(true) | ||
export class User extends SchemaBase { | ||
@String() | ||
username: string; | ||
} | ||
|
||
const user = new User({ | ||
username: 'AmauryD' | ||
}); | ||
const validated = user.validate(); | ||
``` | ||
|
||
### After | ||
|
||
```ts | ||
@Schema({ | ||
strict: true | ||
}) | ||
export class User { | ||
@String() | ||
username: string; | ||
} | ||
const validated = validateFromPayload(User, { | ||
username: 'AmauryD' | ||
}); | ||
``` | ||
|
||
|
||
## Fastest-validator is now a peerDependency | ||
|
||
`fastest-validator` has been moved to peerDependencies. So it needs to be installed in your project using your favorite package manager. | ||
|
||
## Decorators are only applying "type" property as default. | ||
|
||
The old decorators were applying some properties by default (like converting numbers). This behavior was confusing and was removed. User needs to explicitly tell fastest validator to apply a specific validation option. | ||
|
||
### Before | ||
|
||
`@Number()` | ||
|
||
- applies `{ type: "number", convert: true }` | ||
|
||
### After | ||
|
||
`@Number()` | ||
|
||
- applies `{ type: "number" }` | ||
|
||
|
||
## Schema first option is now an object. | ||
|
||
For better readability the first option to the @Schema decorator is an object. | ||
|
||
### Before | ||
|
||
```ts | ||
// applies strict | ||
@Schema(true) | ||
class X {} | ||
``` | ||
|
||
### After | ||
|
||
```ts | ||
// applies strict | ||
@Schema({ | ||
strict: true | ||
}) | ||
class X {} | ||
``` | ||
|
||
## Schema second option has changed. | ||
|
||
Before, second option was used to create only custom validation messages. It has been replaced by a more flexible option `ValidatorConstructorOptions`. You need to wrap your old message object in a message key in order to migrate. | ||
|
||
### Before | ||
|
||
```ts | ||
// applies strict | ||
@Schema(true, { | ||
evenNumber: "The '{field}' field must be an even number! Actual: {actual}" | ||
}) | ||
class X {} | ||
``` | ||
|
||
### After | ||
|
||
```ts | ||
// applies strict | ||
@Schema({ | ||
strict: true | ||
}, { | ||
messages: { | ||
// Register our new error message text | ||
evenNumber: "The '{field}' field must be an even number! Actual: {actual}" | ||
}, | ||
// other fastest validator options ... | ||
plugins: [], | ||
}) | ||
class X {} | ||
``` | ||
|
||
## Decorators are fully typed. | ||
|
||
All decorators now provide typescript auto-completion. The types are directly from fastest-validator package. |
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
# Fastest Validator Decorators | ||
> Decorators for [fastest-validator](https://github.com/icebob/fastest-validator#readme) | ||
:boom: Coming from 1.x ? See the [Migration Guide](MIGRATING.md). | ||
|
||
## Example usage | ||
|
||
```js | ||
|
@@ -22,7 +24,9 @@ import { | |
validateOrReject | ||
} from "fastest-validator-decorators"; | ||
|
||
@Schema(true) | ||
@Schema({ | ||
strict: true | ||
}) | ||
class Entity1 { | ||
@Array({ items: "string"}) | ||
prop1: string[]; | ||
|
@@ -52,7 +56,7 @@ const schema = getSchema(Entity2); // get the fastest-validator schema | |
prop1: { type: "uuid" }, | ||
prop2: { type: "enum", values: ["one", "two"] }, | ||
prop3: { type: "email" }, | ||
prop4: { type: "number", positive: true, convert: true }, | ||
prop4: { type: "number", positive: true }, | ||
prop5: { type: "object", strict: true, props: { | ||
prop1: { type: "array", items: "string" } | ||
}} | ||
|
@@ -69,28 +73,15 @@ const result = validate(entity); // returns true or fastest-validator errors | |
const result = await validateOrReject(entity); // returns true or throws fastest-validator errors | ||
``` | ||
|
||
The `SchemaBase` utility class reduces the verbosity of instantiating new objects by calling Object.assign in the constructor and provides a validate() method. | ||
|
||
:mega: This `SchemaBase` approach will probably be removed in 2.x release. See [#27](https://github.com/AmauryD/fastest-validator-decorators/issues/27). | ||
|
||
```js | ||
@Schema() | ||
class Entity extends SchemaBase { | ||
@Email() | ||
email: string; | ||
} | ||
|
||
const entity = new Entity({ email: '[email protected]' }); | ||
const result = entity.validate(); | ||
``` | ||
|
||
## Setup | ||
|
||
Install the package | ||
``` | ||
npm install --save fastest-validator-decorators | ||
npm install --save fastest-validator-decorators fastest-validator | ||
``` | ||
|
||
:sparkles: fastest-validator is a peerDependency. | ||
|
||
Add the following to your tsconfig.json | ||
``` | ||
"experimentalDecorators": true | ||
|
@@ -101,16 +92,15 @@ Add the following to your tsconfig.json | |
|
||
All decorators accept an object of options that apply to the type being used, for a full list of options please refer to the fastest-validator [documentation](https://www.npmjs.com/package/fastest-validator). | ||
|
||
**@Schema(strict=false, messages={})** - Schema decorator. | ||
**@Schema({ strict = false, async = false }, messages={})** - Schema decorator. | ||
|
||
**@Field({})** - Generic decorator, no default properties set. Will apply all options to the schema. | ||
|
||
[**@String({})**](https://github.com/icebob/fastest-validator#string) - Applies { type: "string", empty: false } | ||
[**@String({})**](https://github.com/icebob/fastest-validator#string) - Applies { type: "string" } | ||
|
||
[**@Boolean({})**](https://github.com/icebob/fastest-validator#boolean) - Applies { type: "boolean" } | ||
|
||
[**@Number({})**](https://github.com/icebob/fastest-validator#number) - Applies { type: "number", convert: true } | ||
[**@Number({})**](https://github.com/icebob/fastest-validator#number) - Applies { type: "number" } | ||
|
||
[**@UUID({})**](https://github.com/icebob/fastest-validator#uuid) - Applies { type: "uuid" } | ||
|
||
|
@@ -142,6 +132,8 @@ All decorators accept an object of options that apply to the type being used, fo | |
|
||
**@Nested({})** - Applies { type: "object", props: {} } (The props are gathered from the nested schema) | ||
|
||
**@NestedArray({ validator: `<ValidatorSchema>` })** - Applies { type: "array", "items": { type :"object", props: { ... } }} (The props are gathered from the nested schema) | ||
|
||
[**@Custom({})**](https://github.com/icebob/fastest-validator#custom-validator) - Applies { type: "custom" } | ||
|
||
```ts | ||
|
@@ -157,7 +149,7 @@ All decorators accept an object of options that apply to the type being used, fo | |
|
||
## Async support | ||
|
||
In order to a schema to be async , you must add the `async: true` to `SchemaOptions`. | ||
In order to a schema to be async , you must add the `async: true` to [SchemaOptions](https://github.com/AmauryD/fastest-validator-decorators/blob/6e34ebf41eae5ed60bb1f8bf0dc4379e72136f4e/src/schema.ts#L7). | ||
|
||
:warning: Be carefull, when enabling async option, the return of the validate function becomes a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). | ||
|
||
|
@@ -192,5 +184,7 @@ const result = await validate(user); | |
|
||
**validateOrReject()** - Returns true or throws fastest-validator errors for a given instance | ||
|
||
|
||
## License | ||
Licensed under the MIT license. | ||
|
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.