Skip to content

Commit bb25248

Browse files
authored
fix(pinia-orm): boolean Type not casting truthy values (#1994)
* fix(pinia-orm): boolean Type not casting truthy values * refactor(pinia-orm): linting
1 parent 1c02d4e commit bb25248

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

packages/pinia-orm/src/model/attributes/types/Boolean.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Model } from '../../Model'
2+
import { BooleanCast } from '../../casts/BooleanCast'
23
import type { TypeDefault } from './Type'
34
import { Type } from './Type'
45

@@ -14,6 +15,7 @@ export class Boolean extends Type {
1415
* Make the value for the attribute.
1516
*/
1617
make (value: any): boolean | null {
17-
return this.makeReturn<boolean | null>('boolean', value)
18+
const booleanCast = new BooleanCast(value)
19+
return booleanCast.get(this.makeReturn<boolean | null>('boolean', value))
1820
}
1921
}

packages/pinia-orm/tests/unit/model/Model_Attrs_Boolean.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ describe('unit/model/Model_Attrs_Boolean', () => {
1313
}
1414

1515
expect(new User({}).bool).toBe(true)
16-
expect(new User({ bool: '' }).bool).toBe('')
17-
expect(new User({ bool: 'string' }).bool).toBe('string')
18-
expect(new User({ bool: '0' }).bool).toBe('0')
19-
expect(new User({ bool: 0 }).bool).toBe(0)
20-
expect(new User({ bool: 1 }).bool).toBe(1)
16+
expect(new User({ bool: '' }).bool).toBe(false)
17+
expect(new User({ bool: 'string' }).bool).toBe(true)
18+
expect(new User({ bool: '0' }).bool).toBe(false)
19+
expect(new User({ bool: 0 }).bool).toBe(false)
20+
expect(new User({ bool: 1 }).bool).toBe(true)
2121
expect(new User({ bool: true }).bool).toBe(true)
2222
expect(new User({ bool: null }).bool).toBe(null)
2323
})
@@ -33,11 +33,11 @@ describe('unit/model/Model_Attrs_Boolean', () => {
3333
const logger = vi.spyOn(console, 'warn')
3434

3535
expect(new User({}).bool).toBe(null)
36-
expect(new User({ bool: '' }).bool).toBe('')
37-
expect(new User({ bool: 'string' }).bool).toBe('string')
38-
expect(new User({ bool: '0' }).bool).toBe('0')
39-
expect(new User({ bool: 0 }).bool).toBe(0)
40-
expect(new User({ bool: 1 }).bool).toBe(1)
36+
expect(new User({ bool: '' }).bool).toBe(false)
37+
expect(new User({ bool: 'string' }).bool).toBe(true)
38+
expect(new User({ bool: '0' }).bool).toBe(false)
39+
expect(new User({ bool: 0 }).bool).toBe(false)
40+
expect(new User({ bool: 1 }).bool).toBe(true)
4141
expect(new User({ bool: true }).bool).toBe(true)
4242
expect(new User({ bool: null }).bool).toBe(null)
4343
expect(logger).toBeCalledTimes(6)

0 commit comments

Comments
 (0)