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

feat(field): add id field type #52

Merged
merged 6 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ plan.errors // { monthlyCost: [ wrongType: 'Number' ] }

```

You can ignore id field validation using `isValid({exceptIDs: true})`. Example: Imagine that your id should not be null, but sometimes, in an insertion case, the ID only exists after an insertion in the database, so you can validate the hole entity, except the id field.

```javascript

const Plan =
entity('Plan', {
...
myId: id(Number),
monthlyCost: field(Number),
})

const plan = new Plan()
plan.plan.myId = '123'
plan.plan.monthlyCost = 500
plan.isValid({exceptIDs: true}) // true

plan.isValid() // false
plan.errors // { myId: [ wrongType: 'Number' ] }

```


### Custom Validation

Expand Down Expand Up @@ -216,6 +237,28 @@ const User =
})
```

### ID Fields

It is possible to declare a field as an ID. This metadata will be used by glues to enrich the infrastructure interfaces (Database, REST, GraphQL, etc).

We can do it in two ways:

```javascript
// The explicit way
const User =
entity('User', {
id: field(Number, { isId: true }),
...
})

// The short way
const User =
entity('User', {
id: id(Number),
...
})
```

### Default value

It is possible to define a default value when an entity instance is initiate.
Expand Down
Loading