Skip to content

Commit

Permalink
docs: add jsonSchema field
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou authored and Janny committed Apr 16, 2019
1 parent 6ebb283 commit 05bf4ac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/site/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- NOTE(kjdelisle): Until we have a metadata docs section, link to the
package in the repository. -->

Expand Down
28 changes: 26 additions & 2 deletions docs/site/Parsing-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 05bf4ac

Please sign in to comment.