Comparing version 5.5.0 to 5.5.1
@@ -54,2 +54,5 @@ /** | ||
default_language?: string; | ||
lowercase?: boolean; | ||
uppercase?: boolean; | ||
trim?: boolean; | ||
} | ||
@@ -56,0 +59,0 @@ /** |
@@ -32,4 +32,9 @@ import { ObjectID } from 'bson'; | ||
} | ||
export interface TransformStringOptions { | ||
lowercase?: boolean; | ||
uppercase?: boolean; | ||
trim?: boolean; | ||
} | ||
export declare type PropOptionsWithNumberValidate = PropOptions & ValidateNumberOptions; | ||
export declare type PropOptionsWithStringValidate = PropOptions & ValidateStringOptions; | ||
export declare type PropOptionsWithStringValidate = PropOptions & TransformStringOptions & ValidateStringOptions; | ||
export declare type PropOptionsWithValidate = PropOptionsWithNumberValidate | PropOptionsWithStringValidate; | ||
@@ -36,0 +41,0 @@ export declare const prop: (options?: PropOptionsWithValidate) => (target: any, key: string) => void; |
@@ -9,2 +9,3 @@ "use strict"; | ||
const isWithStringValidate = (options) => (options.minlength || options.maxlength || options.match); | ||
const isWithStringTransform = (options) => (options.lowercase || options.uppercase || options.trim); | ||
const isWithNumberValidate = (options) => (options.min || options.max); | ||
@@ -68,2 +69,6 @@ const baseProp = (rawOptions, Type, target, key, isArray = false) => { | ||
} | ||
// check for transform inconsistencies | ||
if (isWithStringTransform(rawOptions) && !utils_1.isString(Type)) { | ||
throw new errors_1.NotStringTypeError(key); | ||
} | ||
const instance = new Type(); | ||
@@ -70,0 +75,0 @@ const subSchema = data_1.schema[instance.constructor.name]; |
{ | ||
"name": "typegoose", | ||
"version": "5.5.0", | ||
"version": "5.5.1", | ||
"description": "Define Mongoose models using TypeScript classes.", | ||
@@ -28,4 +28,4 @@ "main": "lib/typegoose.js", | ||
"dependencies": { | ||
"lodash": "4.17.10", | ||
"mongoose": "^5.4.9", | ||
"lodash": "~4.17.11", | ||
"mongoose": "^5.4.15", | ||
"reflect-metadata": "^0.1.13" | ||
@@ -45,3 +45,3 @@ }, | ||
"chai": "4.1.2", | ||
"coveralls": "^3.0.1", | ||
"coveralls": "^3.0.3", | ||
"dotenv": "5.0.1", | ||
@@ -48,0 +48,0 @@ "istanbul": "0.4.5", |
205
README.md
@@ -104,3 +104,3 @@ # Typegoose | ||
``` | ||
Please note that sub documents doesn't have to extend Typegoose. You can still give them default value in `prop` decorator, but you can't create static or instance methods on them. | ||
Please note that sub documents do not have to extend Typegoose. You can still give them default value in `prop` decorator, but you can't create static or instance methods on them. | ||
@@ -162,87 +162,102 @@ ## Requirements | ||
```typescript | ||
// this is now required in the schema | ||
@prop({ required: true }) | ||
firstName: string; | ||
```typescript | ||
// this is now required in the schema | ||
@prop({ required: true }) | ||
firstName: string; | ||
// by default, a property is not required | ||
@prop() | ||
lastName?: string; // using the ? optional property | ||
``` | ||
// by default, a property is not required | ||
@prop() | ||
lastName?: string; // using the ? optional property | ||
``` | ||
- `index`: Tells Mongoose whether to define an index for the property. | ||
```typescript | ||
@prop({ index: true }) | ||
indexedField?: string; | ||
``` | ||
```typescript | ||
@prop({ index: true }) | ||
indexedField?: string; | ||
``` | ||
- `unique`: Just like the [Mongoose unique](http://mongoosejs.com/docs/api.html#schematype_SchemaType-unique), tells Mongoose to ensure a unique index is created for this path. | ||
```typescript | ||
// this field is now unique across the collection | ||
@prop({ unique: true }) | ||
uniqueId?: string; | ||
``` | ||
```typescript | ||
// this field is now unique across the collection | ||
@prop({ unique: true }) | ||
uniqueId?: string; | ||
``` | ||
```typescript | ||
// this field is now unique across the collection | ||
@prop({ unique: true }) | ||
uniqueId?: string; | ||
``` | ||
- `enum`: The enum option accepts a string array. The class property which gets this decorator should have an enum-like type which values are from the provided string array. The way how the enum is created is delegated to the developer, Typegoose needs a string array which hold the enum values, and a TypeScript type which tells the possible values of the enum. | ||
However, if you use TS 2.4+, you can use string enum as well. | ||
```typescript | ||
// Enum-like type and definition example. | ||
type Gender = 'male' | 'female'; | ||
const Genders = { | ||
MALE: 'male' as Gender, | ||
FEMALE: 'female' as Gender, | ||
}; | ||
```typescript | ||
// Enum-like type and definition example. | ||
type Gender = 'male' | 'female'; | ||
const Genders = { | ||
MALE: 'male' as Gender, | ||
FEMALE: 'female' as Gender, | ||
}; | ||
@prop({ enum: Object.values(Genders) }) | ||
gender?: Gender; | ||
@prop({ enum: Object.values(Genders) }) | ||
gender?: Gender; | ||
// TS 2.4+ string enum example | ||
enum Gender { | ||
MALE = 'male', | ||
FEMALE = 'female', | ||
} | ||
// TS 2.4+ string enum example | ||
enum Gender { | ||
MALE = 'male', | ||
FEMALE = 'female', | ||
} | ||
@prop({ enum: Gender }) | ||
gender?: Gender; | ||
``` | ||
@prop({ enum: Gender }) | ||
gender?: Gender; | ||
``` | ||
- `default`: The provided value will be the default for that Mongoose property. | ||
- `lowercase`: for strings only; whether to always call .toLowerCase() on the value. | ||
```typescript | ||
@prop({ default: 'Nick' }) | ||
@prop({ lowercase: true }) | ||
nickName?: string; | ||
``` | ||
- `ref`: By adding the `ref` option with another Typegoose class as value, a Mongoose reference property will be created. The type of the property on the Typegoose extending class should be `Ref<T>` (see Types section). | ||
- `uppercase`: for strings only; whether to always call .toUpperCase() on the value. | ||
```typescript | ||
class Car extends Typegoose {} | ||
@prop({ ref: Car }) | ||
car?: Ref<Car>; | ||
@prop({ uppercase: true }) | ||
nickName?: string; | ||
``` | ||
- `min` / `max` (numeric validators): Same as [Mongoose numberic validators](http://mongoosejs.com/docs/api.html#schema_number_SchemaNumber-max). | ||
- `trim`: for strings only; whether to always call .trim() on the value. | ||
```typescript | ||
@prop({ min: 10, max: 21 }) | ||
age?: number; | ||
@prop({ trim: true }) | ||
nickName?: string; | ||
``` | ||
- `default`: The provided value will be the default for that Mongoose property. | ||
```typescript | ||
@prop({ default: 'Nick' }) | ||
nickName?: string; | ||
``` | ||
- `ref`: By adding the `ref` option with another Typegoose class as value, a Mongoose reference property will be created. The type of the property on the Typegoose extending class should be `Ref<T>` (see Types section). | ||
```typescript | ||
class Car extends Typegoose {} | ||
@prop({ ref: Car }) | ||
car?: Ref<Car>; | ||
``` | ||
- `min` / `max` (numeric validators): Same as [Mongoose numberic validators](http://mongoosejs.com/docs/api.html#schema_number_SchemaNumber-max). | ||
```typescript | ||
@prop({ min: 10, max: 21 }) | ||
age?: number; | ||
``` | ||
- `minlength` / `maxlength` / `match` (string validators): Same as [Mongoose string validators](http://mongoosejs.com/docs/api.html#schema_string_SchemaString-match). | ||
```typescript | ||
@prop({ minlength: 5, maxlength: 10, match: /[0-9a-f]*/ }) | ||
favouriteHexNumber: string; | ||
``` | ||
```typescript | ||
@prop({ minlength: 5, maxlength: 10, match: /[0-9a-f]*/ }) | ||
favouriteHexNumber: string; | ||
``` | ||
@@ -252,42 +267,42 @@ | ||
```typescript | ||
// you have to get your own `isEmail` function, this is a placeholder | ||
```typescript | ||
// you have to get your own `isEmail` function, this is a placeholder | ||
@prop({ validate: (value) => isEmail(value)}) | ||
email?: string; | ||
@prop({ validate: (value) => isEmail(value)}) | ||
email?: string; | ||
// or | ||
// or | ||
@prop({ validate: (value) => { return new Promise(res => { res(isEmail(value)) }) }) | ||
email?: string; | ||
@prop({ validate: (value) => { return new Promise(res => { res(isEmail(value)) }) }) | ||
email?: string; | ||
// or | ||
// or | ||
@prop({ validate: { | ||
validator: val => isEmail(val), | ||
message: `{VALUE} is not a valid email` | ||
}}) | ||
email?: string; | ||
@prop({ validate: { | ||
validator: val => isEmail(val), | ||
message: `{VALUE} is not a valid email` | ||
}}) | ||
email?: string; | ||
// or | ||
// or | ||
@prop({ validate: /\S+@\S+\.\S+/ }) | ||
email?: string; | ||
@prop({ validate: /\S+@\S+\.\S+/ }) | ||
email?: string; | ||
// you can also use multiple validators in an array. | ||
// you can also use multiple validators in an array. | ||
@prop({ validate: | ||
[ | ||
{ | ||
validator: val => isEmail(val), | ||
message: `{VALUE} is not a valid email` | ||
}, | ||
{ | ||
validator: val => isBlacklisted(val), | ||
message: `{VALUE} is blacklisted` | ||
} | ||
] | ||
}) | ||
email?: string; | ||
``` | ||
@prop({ validate: | ||
[ | ||
{ | ||
validator: val => isEmail(val), | ||
message: `{VALUE} is not a valid email` | ||
}, | ||
{ | ||
validator: val => isBlacklisted(val), | ||
message: `{VALUE} is blacklisted` | ||
} | ||
] | ||
}) | ||
email?: string; | ||
``` | ||
@@ -322,6 +337,6 @@ Mongoose gives developers the option to create [virtual properties](http://mongoosejs.com/docs/api.html#schema_Schema-virtual). This means that actual database read/write will not occur these are just 'calculated properties'. A virtual property can have a setter and a getter. TypeScript also has a similar feature which Typegoose uses for virtual property definitions (using the `prop` decorator). | ||
```typescript | ||
@arrayProp({ items: String }) | ||
languages?: string[]; | ||
``` | ||
```typescript | ||
@arrayProp({ items: String }) | ||
languages?: string[]; | ||
``` | ||
@@ -332,8 +347,8 @@ Note that unfortunately the [reflect-metadata](https://github.com/rbuckton/reflect-metadata) API does not let us determine the type of the array, it only returns `Array` when the type of the property is queried. This is why redundancy is required here. | ||
```typescript | ||
class Car extends Typegoose {} | ||
```typescript | ||
class Car extends Typegoose {} | ||
@arrayProp({ itemsRef: Car }) | ||
previousCars?: Ref<Car>[]; | ||
``` | ||
@arrayProp({ itemsRef: Car }) | ||
previousCars?: Ref<Car>[]; | ||
``` | ||
@@ -340,0 +355,0 @@ ### Method decorators |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41898
564
503
+ Addedlodash@4.17.21(transitive)
- Removedlodash@4.17.10(transitive)
Updatedlodash@~4.17.11
Updatedmongoose@^5.4.15