From 64c376949932d312784d66af7c52b9d26eeb09e1 Mon Sep 17 00:00:00 2001 From: Ashot Nazaryan Date: Mon, 25 Dec 2023 00:37:26 -0800 Subject: [PATCH 1/3] docs: validation for custom methods An example on how to use validators with custom methods --- docs/api/services.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/api/services.md b/docs/api/services.md index 8f3ce5dfdf..843d43c4ec 100644 --- a/docs/api/services.md +++ b/docs/api/services.md @@ -282,6 +282,46 @@ When passing the `methods` option **all methods** you want to expose, including +### 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): Promise> { + 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). From 5b93c7059599e622805aad2e9d212aba879780f3 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Wed, 29 May 2024 12:22:23 -0600 Subject: [PATCH 2/3] docs: move custom method validation to validation page --- docs/api/schema/validators.md | 43 +++++++++++++++++++++++++++++++++++ docs/api/services.md | 40 -------------------------------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/docs/api/schema/validators.md b/docs/api/schema/validators.md index 964ddf16ae..4ccc34a5c0 100644 --- a/docs/api/schema/validators.md +++ b/docs/api/schema/validators.md @@ -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): Promise> { + const { name } = data + return { response: `Hello ${name}` } +} +``` + +Finally, we can add our validator in our service hooks + +```ts +sayHello: [schemaHooks.validateData(sayHelloValidator)] +``` diff --git a/docs/api/services.md b/docs/api/services.md index 843d43c4ec..8f3ce5dfdf 100644 --- a/docs/api/services.md +++ b/docs/api/services.md @@ -282,46 +282,6 @@ When passing the `methods` option **all methods** you want to expose, including -### 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): Promise> { - 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). From 049dba305e622cc83875bb60c2dc2a778f08a282 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Wed, 29 May 2024 12:22:32 -0600 Subject: [PATCH 3/3] docs: fix broken link in api/services page --- docs/api/services.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/services.md b/docs/api/services.md index 8f3ce5dfdf..9d749f69d2 100644 --- a/docs/api/services.md +++ b/docs/api/services.md @@ -8,7 +8,7 @@ Services are the heart of every Feathers application. Services are objects or in ## Service methods -Service methods are pre-defined [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) and [custom methods](#custommethod-data-params) that your service provides or that have already been implemented by one of the [database adapters](./databases/common.md). Below is an example of a Feathers service as a class or object. +Service methods are pre-defined [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) and [custom methods](#custom-methods) that your service provides or that have already been implemented by one of the [database adapters](./databases/common.md). Below is an example of a Feathers service as a class or object. ```ts import { feathers } from '@feathersjs/feathers'