UNPKG

4.46 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const DB = require('./clients').getClient;
5
6const isString = function(s) {
7 return _.isString(s);
8};
9
10const isNumber = function(n) {
11 return _.isNumber(n) && _.isFinite(n) && !isString(n);
12};
13
14const isBoolean = function(b) {
15 return _.isBoolean(b);
16};
17
18const isDate = function(d) {
19 return isNumber(d) || _.isDate(d) || isNumber(Date.parse(d));
20};
21
22const isBuffer = function(b) {
23 return typeof b === 'object' || b instanceof Buffer;
24};
25
26const isObject = function(o) {
27 return _.isObject(o);
28};
29
30const isArray = function(a) {
31 return _.isArray(a);
32};
33
34const isDocument = function(m) {
35 return m && m.documentClass && m.documentClass() === 'document';
36};
37
38const isEmbeddedDocument = function(e) {
39 return e && e.documentClass && e.documentClass() === 'embedded';
40};
41
42const isReferenceable = function(r) {
43 return isDocument(r) || isNativeId(r);
44};
45
46const isNativeId = function(n) {
47 return DB().isNativeId(n);
48};
49
50const isSupportedType = function(t) {
51 return (t === String || t === Number || t === Boolean ||
52 t === Buffer || t === Date || t === Array ||
53 isArray(t) || t === Object || t instanceof Object ||
54 typeof(t.documentClass) === 'function');
55};
56
57const isType = function(value, type) {
58 if (type === String) {
59 return isString(value);
60 } else if (type === Number) {
61 return isNumber(value);
62 } else if (type === Boolean) {
63 return isBoolean(value);
64 } else if (type === Buffer) {
65 return isBuffer(value);
66 } else if (type === Date) {
67 return isDate(value);
68 } else if (type === Array || isArray(type)) {
69 return isArray(value);
70 } else if (type === Object) {
71 return isObject(value);
72 } else if (type.documentClass && type.documentClass() === 'document') {
73 return isDocument(value) || DB().isNativeId(value);
74 } else if (type.documentClass && type.documentClass() === 'embedded') {
75 return isEmbeddedDocument(value);
76 } else if (type === DB().nativeIdType()) {
77 return isNativeId(value);
78 } else {
79 throw new Error('Unsupported type: ' + type.name);
80 }
81};
82
83const isValidType = function(value, type) {
84 // NOTE
85 // Maybe look at this:
86 // https://github.com/Automattic/mongoose/tree/master/lib/types
87
88 // TODO: For now, null is okay for all types. May
89 // want to specify in schema using 'nullable'?
90 if (value === null) return true;
91
92 // Issue #9: To avoid all model members being stored
93 // in DB, allow undefined to be assigned. If you want
94 // unassigned members in DB, use null.
95 if (value === undefined) return true;
96
97 // Arrays take a bit more work
98 if (type === Array || isArray(type)) {
99 // Validation for types of the form [String], [Number], etc
100 if (isArray(type) && type.length > 1) {
101 throw new Error('Unsupported type. Only one type can be specified in arrays, but multiple found:', + type);
102 }
103
104 if (isArray(type) && type.length === 1 && isArray(value)) {
105 let arrayType = type[0];
106 for (let i = 0; i < value.length; i++) {
107 let v = value[i];
108 if (!isType(v, arrayType)) {
109 return false;
110 }
111 }
112 } else if (isArray(type) && type.length === 0 && !isArray(value)) {
113 return false;
114 } else if (type === Array && !isArray(value)) {
115 return false;
116 }
117
118 return true;
119 }
120
121 return isType(value, type);
122};
123
124const isInChoices = function(choices, choice) {
125 if (!choices) {
126 return true;
127 }
128 return choices.indexOf(choice) > -1;
129};
130
131const isEmptyValue = function(value) {
132 return typeof value === 'undefined' || (!(typeof value === 'number' || value instanceof Date || typeof value === 'boolean')
133 && (0 === Object.keys(value).length));
134};
135
136exports.isString = isString;
137exports.isNumber = isNumber;
138exports.isBoolean = isBoolean;
139exports.isDate = isDate;
140exports.isBuffer = isBuffer;
141exports.isObject = isObject;
142exports.isArray = isArray;
143exports.isDocument = isDocument;
144exports.isEmbeddedDocument = isEmbeddedDocument;
145exports.isReferenceable = isReferenceable;
146exports.isNativeId = isNativeId;
147exports.isSupportedType = isSupportedType;
148exports.isType = isType;
149exports.isValidType = isValidType;
150exports.isInChoices = isInChoices;
151exports.isEmptyValue = isEmptyValue;
\No newline at end of file