Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat validators loader #21

Merged
merged 14 commits into from
Feb 8, 2022
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- [Service](#service)
- [Middleware](#middleware)
- [Router](#router)
- [Validator](#validator)
- [Module](#module)
- [Decorators](#decorators)
- [Entity event handling](#entity-event-handling)
Expand Down Expand Up @@ -344,6 +345,30 @@ export class ProductRouter {
```
</details>

### Validator

If you add a custom field on an entity, there is a huge risk that you end up getting
an error as soon as you it the end point with that new field. The medusa validators
are not aware of your new field once the request arrive. In order to handle that
you can extend the class validator in order to add your custom field constraint.

<details>
<summary>Click to see the example!</summary>

```typescript
// modules/product/AdminPostProductsReq.validator.ts

import { Validator } from 'medusa-extender';
import { AdminPostProductsReq } from "@medusajs/medusa/dist";

@Validator({ override: AdminPostProductsReq })
class ExtendedClassValidator extends AdminPostProductsReq {
@IsString()
customField: string;
}
```
</details>

### Module

the last step is to import everything in our module :package:
Expand All @@ -369,7 +394,8 @@ import AddFieldToProduct1611063162649 from './product.20211126000001-add-field-t
ProductService,
ProductRouter,
CustomMiddleware,
AddFieldToProduct1611063162649
AddFieldToProduct1611063162649,
ExtendedClassValidator
]
})
export class MyModule {}
Expand Down
2 changes: 1 addition & 1 deletion assets/coverage/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets/coverage/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets/coverage/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion docs/.nojekyll

This file was deleted.

Loading