Skip to content

Commit

Permalink
Merge pull request nestjs#689 from murbanowicz/update-joi-example-to-v16
Browse files Browse the repository at this point in the history
docs(pipes) update Joi example to current version (16)
  • Loading branch information
kamilmysliwiec authored Sep 23, 2019
2 parents 8a080ea + ad2b7a6 commit cf1e339
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions content/pipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ $ npm install --save @hapi/joi
$ npm install --save-dev @types/hapi__joi
```

In the code sample below, we create a simple class that takes a schema as a `constructor` argument. We then apply the `Joi.validate()` method, which validates our incoming argument against the provided schema.
In the code sample below, we create a simple class that takes a schema as a `constructor` argument. We then apply the `schema.validate()` method, which validates our incoming argument against the provided schema.

As noted earlier, a **validation pipe** either returns the value unchanged, or throws an exception.

Expand All @@ -146,23 +146,21 @@ In the next section, you'll see how we supply the appropriate schema for a given

```typescript
@@filename()
import * as Joi from '@hapi/joi';
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';

@Injectable()
export class JoiValidationPipe implements PipeTransform {
constructor(private readonly schema: Object) {}

transform(value: any, metadata: ArgumentMetadata) {
const { error } = Joi.validate(value, this.schema);
const { error } = this.schema.validate(value);
if (error) {
throw new BadRequestException('Validation failed');
}
return value;
}
}
@@switch
import * as Joi from '@hapi/joi';
import { Injectable, BadRequestException } from '@nestjs/common';

@Injectable()
Expand All @@ -172,7 +170,7 @@ export class JoiValidationPipe {
}

transform(value, metadata) {
const { error } = Joi.validate(value, this.schema);
const { error } = this.schema.validate(value);
if (error) {
throw new BadRequestException('Validation failed');
}
Expand Down

0 comments on commit cf1e339

Please sign in to comment.