UNPKG

1.11 kBJavaScriptView Raw
1"use strict";
2
3var Rules = spice.classes.validate.Rules;
4
5Rules.prototype.shouldExist = function* (field, value, args, message) {
6 try {
7 var loaded = require(spice.root_path + "/models/".concat(args));
8
9 var Class = loaded.default;
10 var obj = new Class();
11 var found = yield obj.exist(value);
12
13 if (found) {
14 return true;
15 }
16 } catch (e) {
17 this.validator.addError(field, 'rule', field + 'ShouldExist', 'Cannot Validate ' + args);
18 }
19
20 return false;
21};
22
23Rules.prototype.unique = function* (field, value, args, message) {
24 try {
25 var loaded = require(spice.root_path + "/models/".concat(args[0]));
26
27 var Class = loaded.default;
28 var obj = new Class();
29 var found = yield obj.list({
30 query: "".concat(args[1], " = \"").concat(value, "\"")
31 });
32
33 if (found.data.length == 0) {
34 return true;
35 } else {
36 this.validator.addError(field, 'rule', field + ' should by unique', 'Cannot Validate ' + args[0]);
37 }
38 } catch (e) {
39 this.validator.addError(field, 'rule', field + ' should by unique', 'Cannot Validate ' + args[0]);
40 }
41
42 return false;
43};
\No newline at end of file