From 05bf4ac995d63330a77deb1c1d9b8a8bb7164fb2 Mon Sep 17 00:00:00 2001 From: jannyHou Date: Mon, 15 Apr 2019 18:07:24 -0400 Subject: [PATCH] docs: add jsonSchema field --- docs/site/Model.md | 24 ++++++++++++++++++++++++ docs/site/Parsing-requests.md | 28 ++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/docs/site/Model.md b/docs/site/Model.md index e542918a4440..95692a412fe5 100644 --- a/docs/site/Model.md +++ b/docs/site/Model.md @@ -215,6 +215,30 @@ The complete list of valid attributes for property definitions can be found in LoopBack 3's [Model definition section](https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html#properties). +You can also specify the validation rules in the field `jsonSchema`. For +example: + +```ts +@model() +class Product extends Entity { + @property({ + name: 'name', + description: "The product's common name.", + type: 'string', + // Specify the JSON validation rules here + jsonSchema: { + maxLength: 30, + minLength: 10, + }, + }) + public name: string; +} +``` + +Check out the documentation of +[Parsing requests](Parsing-requests.md#request-body) to see how to do it in +details. + diff --git a/docs/site/Parsing-requests.md b/docs/site/Parsing-requests.md index aa64c041be5f..7c1258341402 100644 --- a/docs/site/Parsing-requests.md +++ b/docs/site/Parsing-requests.md @@ -179,8 +179,32 @@ in/by the `@requestBody` decorator. Please refer to the documentation on [@requestBody decorator](Decorators.md#requestbody-decorator) to get a comprehensive idea of defining custom validation rules for your models. -We support `json`, `urlencoded`, and `text` content types. The client should set -`Content-Type` http header to `application/json`, +You can also specify the JSON schema validation rules in the model property +decorator. The rules are added in a field called `jsonSchema`, like: + +```ts +@model() +class Product extends Entity { + @property({ + name: 'name', + description: "The product's common name.", + type: 'string', + // Specify the JSON validation rules here + jsonSchema: { + maxLength: 30, + minLength: 10, + }, + }) + public name: string; +} +``` + +A full list of validation keywords could be found in the +[documentation of AJV validation keywords](https://github.com/epoberezkin/ajv#validation-keywords). + +One request body specification could contain multiple content types. Our +supported content types are `json`, `urlencoded`, and `text`. The client should +set `Content-Type` http header to `application/json`, `application/x-www-form-urlencoded`, or `text/plain`. Its value is matched against the list of media types defined in the `requestBody.content` object of the OpenAPI operation spec. If no matching media types is found or the type is