title | redirect_from | |
---|---|---|
Array Prop |
|
@arrayProp(options: object)
is almost the same as @prop
, here are just the differences listed
Please note that arrayProp (by mongoose default) initializes the array with []
instead of an null
/ undefined
All options from [@prop
]({{ site.baseurl }}{% link _docs/decorators/prop.md%}#options) are valid, except the ones listed on the bottom
Accepts Type: any
This will tell Typegoose that this is an array which consists of primitives (if String
, Number
, or other primitive type is given) or this is an array which consists of subdocuments (if it's extending the Typegoose
class).
class Something {
@arrayProp({ items: String })
public languages?: string[];
}
Accepts Type: Class | String
(String of the modelName)
Same as [@prop
's ref
]({{ site.baseurl }}{% link _docs/decorators/prop.md %}#ref)
class Car extends Typegoose {}
// in another class
class Something {
@arrayProp({ itemsRef: Car })
public previousCars?: Ref<Car>[];
}
Accepts Type: string
Same as [@prop
's refpath
]({{ site.baseurl }}{% link _docs/decorators/prop.md %}#refPath)
class Car extends Typegoose {}
class Shop extends Typegoose {}
// in another class
class Another extends Typegoose {
@prop({ required: true, enum: 'Car' | 'Shop' })
public which!: string;
@arrayProp({ itemsRefPath: 'which' })
public items?: Ref<Car | Shop>[];
}
innerOptions
is used to overwrite here the options in this object go
-> Use this only when absolutly needed and please open a new issue about it - or for plugins
Example:
class Something {
@arrayProp({ required: true })
public propy: string;
}
// This would be mapped to
{
type: [{ type: String }],
required: true
}
// when using the overwrite
class Something {
@arrayProp({ innerOptions: { required: true } })
public propy: string;
}
// This would be mapped to
{
type: [{ type: String, required: true }]
}
outerOptions
is used to overwrite here the options in this object go
-> Use this only when absolutly needed and please open a new issue about it - or for plugins
Example:
class Something {
@arrayProp({ maxlength: 1 })
public propy: string;
}
// This would be mapped to
{
type: [{ type: String, maxlength: 1 }]
}
// when using the overwrite
class Something {
@arrayProp({ outerOptions: { maxlength: 1 } })
public propy: string;
}
// This would be mapped to
{
type: [{ type: String }],
maxlength: 1
}
ref
does not work, instead useitemsRef
(please let us know if you are interested in combining them into one)refPath
does not work, instead useitemsRefPath
(please let us know if you are interested in combining them into one)
- All String-Validate & Transfrom options look here why
- All Number-Validate options look here why