-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrimitive.ts
More file actions
34 lines (28 loc) · 1.01 KB
/
Primitive.ts
File metadata and controls
34 lines (28 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Model, Type } from '@vuex-orm/core'
import PropertyDecorator from '../contracts/PropertyDecorator'
import { TypeOptions } from '../options/Options'
import Field from './Field'
type Callback = (model: typeof Model) => Type
/**
* Create a generic type decorator.
*/
export function Primitive (callback: Callback, options?: TypeOptions): PropertyDecorator {
return Field((model, propertyKey) => {
const type = callback(model)
if (
typeof type?.value !== 'number' // if that number is 0, what will happen?! always remember it was coerced to false
&& !(type?.value || options?.nullable)
) {
throw new Error(`
[Vuex ORM] You've defined the default value of a field as \`null\` without enabling \`nullable\` option.
If you want the field to accept\`null\`, set \`nullable\` option to \`true\`.
Problematic class name: ${model.name}; Related property key: ${propertyKey}
`)
}
if (options?.nullable) {
type.nullable()
}
return type
})
}
export default Primitive