Skip to content

Stricter field type, and nullable by default #95

Open
@kiaking

Description

@kiaking

This is still just an idea, so looking for feedback.

What do you think about making model attributes more strict? Currently, we have several fields with primitive types, such as string, number. etc. At the moment, these fields will cast values rather than restrict them. So passing true to string field will make it 'true'.

The idea is to make them fail instead of casting.

class User extends Model {
  static entity = 'users'

  @Str() name!: string
}

new User({ name: true }) // <- Error('name is not string')

In addition to this, how about if we make all the fields nullable by default. Because in the frontend, usually many fields are going to be nullable.

So instead of having nullable option, we'll add NonNullable decorator (if this is possible).

class User extends Model {
  static entity = 'users'

  // This field is number, and not nullable.
  @Num()
  @NonNullable()
  id!: number

  // This field is string, or null.
  @Str()
  name!: string | null
}

The default value should behave the same,

class User extends Model {
  static entity = 'users'

  // This field is nullable, but if we pass `null`, it becomes `John Doe`.
  // so technically it wouldn't be `null`.
  @Str('John Doe')
  name!: string
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions