-
Notifications
You must be signed in to change notification settings - Fork 279
/
unique.ts
30 lines (25 loc) · 865 Bytes
/
unique.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { addAttributeOptions } from '../attribute-service';
type UniqueOptions = boolean | string | { name: string; msg: string };
/**
* Sets unique option as specified in options and returns decorator
*/
export function Unique(options: UniqueOptions): Function;
/**
* Decorator, which sets unique option true for annotated property.
*/
export function Unique(target: Object, propertyName: string): void;
export function Unique(...args: any[]): void | Function {
if (args.length === 1) {
const [options] = args;
return (_target, _propertyName) => {
annotate(_target, _propertyName, options);
};
}
const [target, propertyName] = args;
annotate(target, propertyName);
}
function annotate(target: Object, propertyName: string, option: UniqueOptions = true) {
addAttributeOptions(target, propertyName, {
unique: option,
});
}