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/#203 support nullable nested objects #219

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,16 @@ When using Typescript, you can manually set the type of the property when it can
```typescript
import { factory, primaryKey, nullable } from '@mswjs/data'

type Car = {
brand: string
model: string
}

const db = factory({
user: {
id: primaryKey(String),
age: nullable<number>(() => null),
car: nullable<Car>(() => null),
},
})
```
Expand Down
5 changes: 4 additions & 1 deletion src/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export type KeyType = string | number | symbol
export type AnyObject = Record<KeyType, any>
export type PrimaryKeyType = string | number
export type PrimitiveValueType = string | number | boolean | Date
export type ModelValueType = PrimitiveValueType | PrimitiveValueType[]
export type ModelValueType =
| PrimitiveValueType
| PrimitiveValueType[]
| AnyObject
export type ModelValueTypeGetter = () => ModelValueType

export type ModelDefinition = Record<string, ModelDefinitionValue>
Expand Down
6 changes: 5 additions & 1 deletion src/utils/isModelValueType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ function isPrimitiveValueType(value: any): value is PrimitiveValueType {
}

export function isModelValueType(value: any): value is ModelValueType {
return isPrimitiveValueType(value) || Array.isArray(value)
return (
isPrimitiveValueType(value) ||
Array.isArray(value) ||
typeof value === 'object'

Choose a reason for hiding this comment

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

Unsure if this is too permissive?

)
}
64 changes: 64 additions & 0 deletions test/model/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,70 @@ test('creates a new entity with nullable properties', () => {
expect(user.address.number).toEqual(null)
})

test('supports nested nullable object', () => {
type Car = {
brand: string
model: string
}
type Bike = Car

const db = factory({
user: {
id: primaryKey(datatype.uuid),
name: name.findName,
vehicule: {
car: nullable<Car>(() => null),
bike: nullable<Bike>(() => null),
},
},
})

const DIDDY = {
id: '🐵',
name: 'Diddy',
vehicule: {
car: {
brand: 'Diddy Kong',
model: 'Super Racing Model with extra 🍌 carriage',
},
},
}

const diddyUser = db.user.create({
id: DIDDY.id,
name: DIDDY.name,
vehicule: { car: DIDDY.vehicule.car },
})

expect(diddyUser.id).toEqual(DIDDY.id)
expect(diddyUser.name).toEqual(DIDDY.name)
expect(diddyUser.vehicule.car).toEqual(DIDDY.vehicule.car)
expect(diddyUser.vehicule.bike).toEqual(null)

const DONKEY = {
id: '🦍',
name: 'Donkey',
vehicule: {
car: null,
bike: {
brand: 'Donkey Kong Choppers',
model: 'Chuck Norris, the fastest',
},
},
}

const donkeyUser = db.user.create({
id: DONKEY.id,
name: DONKEY.name,
vehicule: DONKEY.vehicule,
})

expect(donkeyUser.id).toEqual(DONKEY.id)
expect(donkeyUser.name).toEqual(DONKEY.name)
expect(donkeyUser.vehicule.bike).toEqual(DONKEY.vehicule.bike)
expect(donkeyUser.vehicule.car).toEqual(null)
})

test('supports nested objects in the model definition', () => {
const db = factory({
user: {
Expand Down