Skip to content

Commit

Permalink
fix #69: Improve documentation for Validate decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Mar 28, 2017
1 parent a2fe04b commit cc702d7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ Custom validators can be added either using the [`validators`](http://sierrasoft
or by using the [`@Validate`](http://sierrasoftworks.github.io/Iridium/globals.html#validate) decorator on your instance class.

```typescript
@Iridium.Validate('myValidator', x => x === 42)
@Iridium.Validate('myValidator', function(schema, data, path) {
return this.assert(data == 42)
})
export class InstanceType extends Iridium.Instance<any, InstanceType> {
@Iridium.Property('myValidator')
myProperty: number;
Expand Down
11 changes: 8 additions & 3 deletions example/UserModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ = require("lodash");
import Promise = require("bluebird");
import * as Iridium from "../iridium";
import {Index, Property} from "../iridium";
import {Index, Property, Validate} from "../iridium";

var settings: any = {};

Expand Down Expand Up @@ -43,7 +43,12 @@ export interface UserDocument {
@Iridium.Collection("user")
@Index({ email: 1 }, { unique: true })
@Index({ sessions: 1 }, { sparse: true })
@Index({ "skill.xp": -1 }, { background: true })
@Index({ "skill.xp": -1 }, { background: true })
@Validate("password", function(schema, data, path) {
// This should use something like zxcvbn to determine whether a password
// is strong enough for your use case.
return this.assert(typeof data === "string" && /^.{8,}$/.test(data), "Expected password to be at least 8 characters long.");
})
export class User extends Iridium.Instance<UserDocument, User> implements UserDocument {
@Iridium.Property(/^[a-z][a-z0-9_]{7,}$/) _id: string;
get username() {
Expand All @@ -52,7 +57,7 @@ export class User extends Iridium.Instance<UserDocument, User> implements UserDo

@Property(String) fullname: string;
@Property(/^.+@.+$/) email: string;
@Property(String) password: string;
@Property("password") password: string;
@Property(/Player|Moderator|Admin/) type: string;
@Property(Boolean) banned: boolean;

Expand Down
5 changes: 5 additions & 0 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export function Index(spec: IndexSpecification, options?: MongoDB.IndexOptions)
* This decorator replaces the use of the static validators property on instance implementation
* classes. If your transpiler does not support decorators then you are free to make use of the
* property instead.
*
* @example
* @Iridium.Validate('everything', function(schema, data, path) {
* return this.assert(data == 42, "Expected the answer to life, the universe and everything.");
* })
*/
export function Validate(forType: any, validate: Skmatc.IValidationHandler) {
return function(target: InstanceImplementation<any,any>) {
Expand Down

0 comments on commit cc702d7

Please sign in to comment.