Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
WIP Adding Array Validators
Browse files Browse the repository at this point in the history
- adding Array Validators
- adding tests for Array Validators
  • Loading branch information
hasezoey committed Aug 7, 2019
1 parent 0d68100 commit 2abb37b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { use } from 'chai';
import * as cap from 'chai-as-promised';

import { suite as ArrayValidatorTests } from './tests/arrayValidator.test';
import { suite as BigUserTest } from './tests/biguser.test';
import { suite as IndexTests } from './tests/db_index.test';
import { suite as GCFDTest } from './tests/getClassForDocument.test';
Expand Down Expand Up @@ -28,6 +29,7 @@ describe('Typegoose', () => {
describe('Indexes', IndexTests.bind(this));

describe('String Validators', StringValidatorTests.bind(this));
describe('Array Validators', ArrayValidatorTests.bind(this));

describe('getClassForDocument()', GCFDTest.bind(this));
});
32 changes: 32 additions & 0 deletions test/models/arrayValidators.ts
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);
63 changes: 63 additions & 0 deletions test/tests/arrayValidator.test.ts
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');
});
}

0 comments on commit 2abb37b

Please sign in to comment.