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

Commit

Permalink
add missing string validation options with tests (#293)
Browse files Browse the repository at this point in the history
adding chai-as-promised
  • Loading branch information
hasezoey authored and Ben305 committed Jul 12, 2019
1 parent 61c5948 commit 7418c34
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"@types/mocha": "^5.2.7",
"@types/mongoose": "^5.5.0",
"@types/node": "8.10.0",
"@types/chai-as-promised": "^7.1.0",
"chai": "4.1.2",
"chai-as-promised": "7.1.1",
"coveralls": "^3.0.4",
"mocha": "^6.1.4",
"mongoose": "^5.5.13",
Expand Down
16 changes: 14 additions & 2 deletions src/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ export interface ValidateNumberOptions {
}

export interface ValidateStringOptions {
lowercase?: boolean;
uppercase?: boolean;
trim?: boolean;
match?: RegExp | [RegExp, string];
enum?: string[];
minlength?: number | [number, string];
maxlength?: number | [number, string];
match?: RegExp | [RegExp, string];
}

export interface TransformStringOptions {
Expand All @@ -77,7 +81,15 @@ export type PropOptionsWithStringValidate = PropOptions & TransformStringOptions
export type PropOptionsWithValidate = PropOptionsWithNumberValidate | PropOptionsWithStringValidate | VirtualOptions;

const isWithStringValidate = (options: PropOptionsWithStringValidate) =>
options.minlength || options.maxlength || options.match;
(
options.lowercase
|| options.uppercase
|| options.trim
|| options.match
|| options.enum
|| options.minlength
|| options.maxlength
);

const isWithStringTransform = (options: PropOptionsWithStringValidate) =>
options.lowercase || options.uppercase || options.trim;
Expand Down
43 changes: 42 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import { expect, use } from 'chai';
import * as mongoose from 'mongoose';

import { fail } from 'assert';
import * as cap from 'chai-as-promised';
import { Ref } from '../src/prop';
import { getClassForDocument } from '../src/utils';
import { Genders } from './enums/genders';
Expand All @@ -12,10 +13,13 @@ import { AddressNested, PersonNested, PersonNestedModel } from './models/nested-
import { model as Person } from './models/person';
import { model as Rating } from './models/rating';
import { model as Select } from './models/select';
import { model as StringValidators } from './models/stringValidators';
import { model as User, User as UserType } from './models/user';
import { Virtual, VirtualSub } from './models/virtualprop';
import { connect, disconnect } from './utils/mongooseConnect';

use(cap);

describe('Typegoose', () => {
before(() => connect());
after(() => disconnect());
Expand Down Expand Up @@ -260,6 +264,43 @@ describe('Typegoose', () => {
// wrong type to make typescript happy
expect((items[1].kind as typeof Beverage).isDecaf).to.be.an('undefined');
});
describe('string validators', () => {
// TODO: Specify an identifier for the error messages, e. g. as a regex to the message content.
// Otherwise we could catch errors that have nothing to do with what is being tested.

it('should respect maxlength', (done) => {
expect(StringValidators.create({
maxLength: 'this is too long',
})).to.eventually.rejectedWith(mongoose.Error).and.notify(done);
});

it('should trim', async () => {
const trimmed = await StringValidators.create({
trimmed: 'trim my end ',
});
expect(trimmed.trimmed).equals('trim my end');
});

it('should uppercase', async () => {
const uppercased = await StringValidators.create({
uppercased: 'make me uppercase',
});
expect(uppercased.uppercased).equals('MAKE ME UPPERCASE');
});

it('should lowercase', async () => {
const lowercased = await StringValidators.create({
lowercased: 'MAKE ME LOWERCASE',
});
expect(lowercased.lowercased).equals('make me lowercase');
});

it('should respect enum', (done) => {
expect(StringValidators.create({
enumed: 'i am not valid',
})).to.eventually.rejectedWith(mongoose.Error).and.notify(done);
});
});

it('should only return selected types', async () => {
const selecttest = new Select();
Expand Down
20 changes: 20 additions & 0 deletions test/models/stringValidators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { prop, Typegoose } from '../../src/typegoose';

export class StringValidators extends Typegoose {
@prop({ maxlength: 3 })
public maxLength: string;

@prop({ trim: true })
public trimmed: string;

@prop({ uppercase: true })
public uppercased: string;

@prop({ lowercase: true })
public lowercased: string;

@prop({ enum: ['one', 'two', 'three'] })
public enumed: string;
}

export const model = new StringValidators().getModelForClass(StringValidators);

0 comments on commit 7418c34

Please sign in to comment.