Skip to content

Commit

Permalink
docs: move custom method validation to validation page
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallswain committed May 29, 2024
1 parent 64c3769 commit ebdaa02
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
43 changes: 43 additions & 0 deletions docs/api/schema/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,46 @@ app.service('messages').hooks({
}
})
```

### Using validators with custom methods

You can optionally create validators for your custom methods. For example we will create a custom method in our `user` service that simply says "Hello ${name}" to the requestor.

For the example we can use this TypeBox schema

```ts
//Our request object, we expect something like {name: "Bob"}
export const sayHelloRequest = Type.Object(
{
name: Type.String({
description: "Who are we saying hello to!",
examples: ["Bob"],
minLength: 2,
}),
},
{ $id: "sayHelloRequest", additionalProperties: false },
);

//We intend on returning an object with a string response property
export const sayHelloResponse = Type.Object(
{ response: Type.String() },
{ $id: "sayHelloResponse", additionalProperties: false },
);

export const sayHelloValidator = getValidator(sayHelloRequest, dataValidator);
```

In our user class file, we can define our custom method

```ts
async sayHello(data: Static<typeof sayHelloRequest>): Promise<Static<typeof sayHelloResponse>> {
const { name } = data
return { response: `Hello ${name}` }
}
```

Finally, we can add our validator in our service hooks

```ts
sayHello: [schemaHooks.validateData(sayHelloValidator)]
```
40 changes: 0 additions & 40 deletions docs/api/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,46 +282,6 @@ When passing the `methods` option **all methods** you want to expose, including

</BlockQuote>

### Using validators with custom methods

You can optionally create validators for your custom methods. For example we will create a custom method in our `user` service that simply says "Hello ${name}" to the requestor.

For the example we can use this TypeBox schema
```ts

//Our request object, we expect something like {name: "Bob"}
export const sayHelloRequest = Type.Object(
{
name: Type.String({
description: "Who are we saying hello to!",
examples: ["Bob"],
minLength: 2,
}),
},
{ $id: "sayHelloRequest", additionalProperties: false },
);

//We intend on returning an object with a string response property
export const sayHelloResponse = Type.Object(
{ response: Type.String() },
{ $id: "sayHelloResponse", additionalProperties: false },
);

export const sayHelloValidator = getValidator(sayHelloRequest, dataValidator);
```

In our user class file, we can define our custom method
```ts
async sayHello(data: Static<typeof sayHelloRequest>): Promise<Static<typeof sayHelloResponse>> {
const { name } = data
return { response: `Hello ${name}` }
}
```

Finally, we can add our validator in our service hooks
`sayHello: [schemaHooks.validateData(sayHelloValidator)]`


## Feathers functionality

When registering a service, Feathers (or its plugins) can also add its own methods to a service. Most notably, every service will automatically become an instance of a [NodeJS EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter).
Expand Down

0 comments on commit ebdaa02

Please sign in to comment.