Skip to content

Commit

Permalink
docs: Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jan 2, 2022
1 parent b0c3e2d commit 68337c4
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ParamMetadata, PipeMethods} from "@tsed/platform-params";
import {Inject, Injectable} from "@tsed/di";
import {NotFound} from "@tsed/exceptions";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {IUsePersonParamOptions} from "../decorators/UsePersonParam";
import {PersonModel} from "../models/PersonModel";
import {PersonsService} from "../models/PersonsService";
Expand All @@ -10,7 +10,7 @@ export class PersonPipe implements PipeMethods<string, Promise<PersonModel>> {
@Inject()
personsService: PersonsService;

async transform(id: string, metadata: ParamMetadata): Promise<PersonModel> {
async transform(id: string, metadata: JsonParameterStore): Promise<PersonModel> {
const person = await this.personsService.findOne(id);
const options = metadata.store.get<IUsePersonParamOptions>(PersonPipe);

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/snippets/pipes/async-transformer-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ParamMetadata, PipeMethods} from "@tsed/platform-params";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {Inject, Injectable} from "@tsed/di";
import {NotFound} from "@tsed/exceptions";
import {PersonModel} from "../models/PersonModel";
Expand All @@ -9,7 +9,7 @@ export class PersonPipe implements PipeMethods<string, Promise<PersonModel>> {
@Inject()
personsService: PersonsService;

async transform(id: string, metadata: ParamMetadata): Promise<PersonModel> {
async transform(id: string, metadata: JsonParameterStore): Promise<PersonModel> {
const person = await this.personsService.findOne(id);

if (!person) {
Expand Down
6 changes: 4 additions & 2 deletions docs/docs/snippets/pipes/transformer-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {ValidationError, PipeMethods, ParamMetadata} from "@tsed/platform-params";
import {ValidationError} from "@tsed/platform-params";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {Injectable} from "@tsed/di";


@Injectable()
export class ParseIntPipe implements PipeMethods<string, number> {
transform(value: string, metadata: ParamMetadata): number {
transform(value: string, metadata: JsonParameterStore): number {
const val = parseInt(value, 10);
if (isNaN(val)) {
throw new ValidationError("Value must an integer or a parsable integer");
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/snippets/pipes/validation-pipe-identity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {ParamMetadata, PipeMethods} from "@tsed/platform-params";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {Injectable} from "@tsed/di";

@Injectable()
export class ValidationPipe implements PipeMethods {
transform(value: any, metadata: ParamMetadata) {
transform(value: any, metadata: JsonParameterStore) {
return value;
}
}
6 changes: 3 additions & 3 deletions docs/docs/snippets/pipes/validation-pipe-with-ajv.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {PipeMethods, ParamMetadata, ValidationError} from "@tsed/platform-params";
import {getJsonSchema} from "@tsed/schema";
import {ValidationError} from "@tsed/platform-params";
import {getJsonSchema, JsonParameterStore, PipeMethods} from "@tsed/schema";
import {Injectable} from "@tsed/di";
import * as Ajv from "ajv";

@Injectable()
export class AjvValidationPipe implements PipeMethods {
ajv = new Ajv();

transform(value: any, metadata: ParamMetadata): any {
transform(value: any, metadata: JsonParameterStore): any {
const schema = getJsonSchema(metadata.type);

if (!this.ajv.validate(schema, value)) {
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/snippets/validation/class-transformer-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {DeserializerPipe, PipeMethods, ParamMetadata} from "@tsed/platform-params";
import {DeserializerPipe} from "@tsed/platform-params";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {OverrideProvider} from "@tsed/di";
import {plainToClass} from "class-transformer";

@OverrideProvider(DeserializerPipe)
export class ClassTransformerPipe implements PipeMethods {
transform(value: any, metadata: ParamMetadata) {
transform(value: any, metadata: JsonParameterStore) {
return plainToClass(metadata.type, value);
}
}
7 changes: 4 additions & 3 deletions docs/docs/snippets/validation/class-validator-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {PipeMethods, ParamMetadata, ValidationError, ValidationPipe} from "@tsed/platform-params";
import {ValidationError, ValidationPipe} from "@tsed/platform-params";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {OverrideProvider} from "@tsed/di";
import {plainToClass} from "class-transformer";
import {validate} from "class-validator";

@OverrideProvider(ValidationPipe)
export class ClassValidationPipe extends ValidationPipe implements PipeMethods<any> {
async transform(value: any, metadata: ParamMetadata) {
async transform(value: any, metadata: JsonParameterStore) {
if (!this.shouldValidate(metadata)) { // there is no type and collectionType
return value;
}
Expand All @@ -20,7 +21,7 @@ export class ClassValidationPipe extends ValidationPipe implements PipeMethods<a
return value;
}

protected shouldValidate(metadata: ParamMetadata): boolean {
protected shouldValidate(metadata: JsonParameterStore): boolean {
const types: Function[] = [String, Boolean, Number, Array, Object];

return !(metadata.type || metadata.collectionType) || !types.includes(metadata.type);
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/snippets/validation/joi-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {ObjectSchema} from "@hapi/joi";
import {Injectable} from "@tsed/di";
import {PipeMethods, ParamMetadata, ValidationError} from "@tsed/platform-params";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {ValidationError} from "@tsed/platform-params";

@Injectable()
export class JoiValidationPipe implements PipeMethods {
transform(value: any, metadata: ParamMetadata) {
transform(value: any, metadata: JsonParameterStore) {
const schema = metadata.store.get<ObjectSchema>(JoiValidationPipe);

if (schema) {
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/snippets/validation/validator-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {PipeMethods, ParamMetadata, ValidationError, ValidationPipe} from "@tsed/platform-params";
import {ValidationError, ValidationPipe} from "@tsed/platform-params";
import {JsonParameterStore, PipeMethods} from "@tsed/schema";
import {OverrideProvider} from "@tsed/di";
import {getJsonSchema} from "@tsed/schema";
import {validate} from "./validate";

@OverrideProvider(ValidationPipe)
export class CustomValidationPipe extends ValidationPipe implements PipeMethods {
public transform(obj: any, metadata: ParamMetadata): void {
public transform(obj: any, metadata: JsonParameterStore): void {
// JSON service contain tool to build the Schema definition of a model.
const schema = getJsonSchema(metadata.type);

Expand Down

0 comments on commit 68337c4

Please sign in to comment.