UNPKG

1.42 kBJavaScriptView Raw
1/**
2 * Module dependencies.
3 */
4
5var _ = require('lodash'),
6 SchemaType = require('../schematype');
7
8
9/**
10 * Creates a new SchemaString instance.
11 *
12 * @param {Object} [options]
13 * @api public
14 */
15
16var SchemaString = module.exports = function(options){
17 SchemaType.call(this, options);
18
19 if (options && options.ref) this.ref = options.ref;
20};
21
22SchemaString.type = SchemaString.prototype.type = String;
23
24/**
25 * Inherits from SchemaType.
26 */
27
28SchemaString.__proto__ = SchemaType;
29SchemaString.prototype.__proto__ = SchemaType.prototype;
30
31/**
32 * Checks if the given `value` is a string.
33 *
34 * @param {any} value
35 * @return {Boolean}
36 * @api public
37 */
38
39SchemaString.prototype.checkRequired = function(value){
40 return value instanceof String || typeof value === 'string';
41};
42
43/**
44 * Casts the given `value` to a string.
45 *
46 * @param {any} value
47 * @return {String}
48 * @api public
49 */
50
51SchemaString.prototype.cast = function(value){
52 if (value == null) return value;
53 if (value instanceof String || typeof value === 'string') return value;
54
55 if (value.toString){
56 return value.toString();
57 } else {
58 return null;
59 }
60};
61
62/**
63 * Compares data.
64 *
65 * @param {String} data
66 * @param {String} value
67 * @return {Boolean}
68 * @api public
69 */
70
71var compare = SchemaString.prototype.compare = function(data, value){
72 if (value instanceof RegExp){
73 return value.test(data);
74 } else {
75 return data == value;
76 }
77};
\No newline at end of file