loopbackio / loopbackio/loopback-next

Required model property with default value throws error when property not provided in create

オープン
#3,058 コメント 11 件 リアクション 2 件 担当者 1 名 GitHub で見る

@aharbis がすでに取り組んでいます。

2019年6月7日 から。

bug good first issue Hacktoberfest REST Validation
主要言語
TypeScript
スター
5.1k
フォーク
1.1k
平均マージ
2日 21時間
マージ済み PR(30日)
27

説明

## Description / Steps to reproduce / Feature proposal

Given a model property that is defined as `required` with a `default` value, trying to create a new model instance and omitting that property from the request yields a 422 error stating the property is missing.

For example, given the following model property definition:

```ts
@property({
type: 'string',
required: true,
default: 'admin',
})
username: string;
```

Trying to create a model instance and omitting the `username` property yields:

```json
{
"error": {
"statusCode": 422,
"name": "UnprocessableEntityError",
"message": "The request body is invalid. See error object `details` property for more info.",
"code": "VALIDATION_FAILED",
"details": [
{
"path": "",
"code": "required",
"message": "should have required property 'username'",
"info": {
"missingProperty": "username"
}
}
]
}
}
```

I've recreated the issue using the `todo` application, and you can view that on my forked feature branch:

https://github.com/aharbis/loopback-next/tree/default-property

I am using Node `v10.15.3`, and based the recreate on an up-to-date fork of `loopback-next`.

## Current Behavior

The `default` value defined for the model property is not utilized, and instead a 422 error is thrown stating the required property is missing.

## Expected Behavior

~If a `required` property has a `default` value, and the given property is not provided in the request to create the model instance, the `default` value should be used.~

Finalized agreement:
LoopBack follow the OpenAPI standard and make no interpretations.
`required` means the user needs to provide the field in the request always, independent of the `default`.

i.e `default: xxx, required: true` shouldn't be set at the same time.

## Accpetance Criteria
- [ ] Update the docs `Models.md`, add explanations and examples
- [ ] Add validation to check if the two fields is set at the runtime

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。