From cc702d723a0418e9363bc64646d78e88995c36cd Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Tue, 28 Mar 2017 19:49:40 +0100 Subject: [PATCH] fix #69: Improve documentation for Validate decorator --- README.md | 4 +++- example/UserModel.ts | 11 ++++++++--- lib/Decorators.ts | 5 +++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cc3a94e..7a090ef 100644 --- a/README.md +++ b/README.md @@ -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 { @Iridium.Property('myValidator') myProperty: number; diff --git a/example/UserModel.ts b/example/UserModel.ts index ca73a30..ca65f16 100644 --- a/example/UserModel.ts +++ b/example/UserModel.ts @@ -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 = {}; @@ -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 implements UserDocument { @Iridium.Property(/^[a-z][a-z0-9_]{7,}$/) _id: string; get username() { @@ -52,7 +57,7 @@ export class User extends Iridium.Instance 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; diff --git a/lib/Decorators.ts b/lib/Decorators.ts index de112fa..756ce59 100644 --- a/lib/Decorators.ts +++ b/lib/Decorators.ts @@ -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) {