Skip to content

Commit

Permalink
Rebuilt ObjectID decorator to make use of default validators
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jun 12, 2015
1 parent 1e686e7 commit 074d91a
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,6 @@ export function Property(...args: any[]): (target: any, name?: string) => void {
}
}

export function ObjectID(target: { constructor: typeof Instance }, name: string) {
target.constructor.schema = target.constructor.schema || <Schema>{};
target.constructor.schema[name] = { $required: false, $type: /^[0-9a-f]{24}$/ };
target.constructor.identifier = {
apply: function(value) {
return (value && value._bsontype == 'ObjectID') ? new MongoDB.ObjectID(value.id).toHexString() : value;
},
reverse: function (value) {
if (value === null || value === undefined) return undefined;
if (value && /^[a-f0-9]{24}$/.test(value)) return MongoDB.ObjectID.createFromHexString(value);
return value;
}
export function Transform(fromDB: (value: any) => any, toDB: (value: any) => any) {
return function(target: any, property: string) {
target.constructor.transforms = _.clone(target.constructor.transforms || {})
Expand All @@ -69,4 +57,9 @@ export function Transform(fromDB: (value: any) => any, toDB: (value: any) => any
toDB: toDB
};
};
}

export function ObjectID(target: { constructor: typeof Instance }, name: string) {
target.constructor.schema = <Schema>_.clone(target.constructor.schema || {});
target.constructor.schema[name] = MongoDB.ObjectID;
}

0 comments on commit 074d91a

Please sign in to comment.