This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adding Array Validators - adding tests for Array Validators
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { arrayProp, Typegoose } from '../../src/typegoose'; | ||
|
||
export class ArrayValidators extends Typegoose { | ||
// @ts-ignore | ||
@arrayProp({ items: String, maxlength: 3 }) | ||
public maxLength: string[]; | ||
|
||
// @ts-ignore | ||
@arrayProp({ items: String, minlength: 10 }) | ||
public minLength: string[]; | ||
|
||
// @ts-ignore | ||
@arrayProp({ items: String, trim: true }) | ||
public trimmed: string[]; | ||
|
||
// @ts-ignore | ||
@arrayProp({ items: String, uppercase: true }) | ||
public uppercased: string[]; | ||
|
||
// @ts-ignore | ||
@arrayProp({ items: String, lowercase: true }) | ||
public lowercased: string[]; | ||
|
||
@arrayProp({ items: String, enum: ['one', 'two', 'three'] }) | ||
public enumed: string[]; | ||
|
||
// @ts-ignore | ||
@arrayProp({ items: String, default: ['hello'], lowercase: true }) | ||
public defaulted: string[]; | ||
} | ||
|
||
export const model = new ArrayValidators().getModelForClass(ArrayValidators); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { expect } from 'chai'; | ||
import * as mongoose from 'mongoose'; | ||
|
||
import { model as ArrayValidators } from '../models/arrayValidators'; | ||
|
||
/** | ||
* Function to pass into describe | ||
* ->Important: you need to always bind this | ||
* @example | ||
* ``` | ||
* import { suite as StringValidatorTests } from './stringValidator.test' | ||
* ... | ||
* describe('String Validators', StringValidatorTests.bind(this)); | ||
* ... | ||
* ``` | ||
*/ | ||
export function suite() { | ||
it('should respect maxlength', (done) => { | ||
expect(ArrayValidators.create({ | ||
maxLength: ['this is too long'], | ||
})).to.eventually.rejectedWith(mongoose.Error.ValidationError).and.notify(done); | ||
}); | ||
|
||
it('should respect minlength', (done) => { | ||
expect(ArrayValidators.create({ | ||
minLength: ['too short'], | ||
})).to.eventually.rejectedWith(mongoose.Error.ValidationError).and.notify(done); | ||
}); | ||
|
||
it('should trim', async () => { | ||
const trimmed = await ArrayValidators.create({ | ||
trimmed: ['trim my end '], | ||
}); | ||
expect(trimmed.trimmed[0]).equals('trim my end'); | ||
}); | ||
|
||
it('should uppercase', async () => { | ||
const uppercased = await ArrayValidators.create({ | ||
uppercased: ['make me uppercase'], | ||
}); | ||
expect(uppercased.uppercased[0]).equals('MAKE ME UPPERCASE'); | ||
}); | ||
|
||
it('should lowercase', async () => { | ||
const lowercased = await ArrayValidators.create({ | ||
lowercased: ['MAKE ME LOWERCASE'], | ||
}); | ||
expect(lowercased.lowercased[0]).equals('make me lowercase'); | ||
}); | ||
|
||
it('should respect enum', (done) => { | ||
expect(ArrayValidators.create({ | ||
enumed: ['not in the enum'], | ||
})).to.eventually.rejectedWith(mongoose.Error).and.notify(done); | ||
}); | ||
|
||
it('should lowercase & have a default', async () => { | ||
const defaulted = await ArrayValidators.create({}); | ||
// @ts-ignore | ||
console.log(ArrayValidators.schema.paths.defaulted); | ||
expect(defaulted.defaulted[0]).equals('hello'); | ||
}); | ||
} |