Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json-schema metadata #592

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/en/lb4/Decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ For more information, see the [Dependency Injection](Dependency-Injection.htm) s

#### Property Decorator

Syntax: `@property(definition: PropertyDefinition)`
Syntax: `@property(definition?: PropertyDefinition)`

The property decorator defines metadata for a property on a Model definition.
The format of property definitions can be found in [Property definitions](https://loopback.io/doc/en/lb2/Model-definition-JSON-file.html#properties)
Expand Down
7 changes: 3 additions & 4 deletions pages/en/lb4/Implementing-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,17 @@ LoopBack is agnostic when it comes to accessing databases. You can choose any pa
export class Product extends Entity {
@property({
description: 'The unique identifier for a product',
type: 'number',
id: true,
})
id: number;

@property({type: 'string', required: true})
@property({required: true})
name: string;

@property({type: 'string', required: true})
@property({required: true})
slug: string;

@property({type: 'number', required: true})
@property({required: true})
price: number;

// Add the remaining properties yourself:
Expand Down
4 changes: 2 additions & 2 deletions pages/en/lb4/Repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ import {Entity, model, property} from '@loopback/repository';

@model()
export class Account extends Entity {
@property({type: 'number', id: true})
@property({id: true})
id: number;

@property({type: 'string', required: true})
@property({required: true})
name: string;
}
```
Expand Down
77 changes: 77 additions & 0 deletions pages/en/lb4/Schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,83 @@ class Customer extends Entity {
}
```

### JSON Schema inference
Use the `@loopback/repository-json-schema module` to build a JSON schema from
a decorated model. Type information is inferred from the `@model` and
`@property` decorators. The `@loopback/repository-json-schema` module contains
the `getJsonSchema` function to access the metadata stored by the decorators
to build a matching JSON Schema of your model.

```ts
import {model, property} from '@loopback/repository';
import {getJsonSchema} from '@loopback/repository-json-schema';

@model()
class Category {
@property() name: string;
}

@model()
class Product {
@property({required: true}) name: string;
@property() type: Category;
}

const jsonSchema = getJsonSchema(Product);
```

`jsonSchema` from above would return:

```json
{
"title": "Product",
"properties": {
"name": {
"type": "string"
},
"type": {
"$ref": "#/definitions/Category"
}
},
"definitions": {
"Category": {
"properties": {
"name": {
"type": "string"
}
}
}
},
"required": ["name"]
}
```

If a custom type is specified for a decorated property in a model definition, then
a reference [`$ref`](http://json-schema.org/latest/json-schema-core.html#rfc.section.8)
field is created for it and a `definitions` sub-schema is created at the top-level
of the schema and populated with the type definition by recursively calling
`getJsonSchema` to build its properties. This allows for complex and nested custom
type definition building. The example above illustrates this point by having the
custom type `Category` as a property of our `Product` model definition.

#### Supported JSON keywords

{% include note.html content="

This feature is still a work in progress and is incomplete.

" %}

Following are the supported keywords that can be explicitly passed into the decorators
to better tailor towards the JSON Schema being produced.

| Keywords | Decorator | Type | Default | Description |
|-------------|-------------|---------|--------------|---------------------------------------------------------|
| title | `@model` | string | *model name* | Name of the model |
| description | `@model` | string | | Description of the model |
| array | `@property` | boolean | | Used to specify whether the property is an array or not |
| required | `@property` | boolean | | Used to specify whether the property is required or not |

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add one of those decoration tags we have to this section to say that it's a work-in-progress. There are most likely other JSON schema keywords/features that we'll want to support in the future, so letting people know that it's not yet feature complete in that respect would be helpful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added edits in-line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One additional edit needed.

## Other ORMs
You might decide to use an alternative ORM/ODM in your LoopBack application.
Loopback v4 no longer expects you to provide your data in its own custom Model
Expand Down