Skip to content

Commit 8efaf09

Browse files
committed
lint
1 parent 82adfdd commit 8efaf09

33 files changed

+425
-393
lines changed

.eslintrc

+3-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,3 @@
1-
{
2-
"parser": "babel-eslint",
3-
"extends": "eslint:recommended",
4-
"globals": {
5-
"sinon": true,
6-
"expect": true,
7-
"TestHelpers": true,
8-
},
9-
"env": {
10-
"browser": true,
11-
"node": true,
12-
"mocha": true,
13-
},
14-
"rules": {
15-
"eqeqeq": 0,
16-
"no-loop-func": 0,
17-
"comma-dangle": 0,
18-
"no-eval": 2,
19-
"strict": 0,
20-
"eol-last": 0,
21-
"dot-notation": [2, { "allowKeywords": true }],
22-
"semi": [0, "never"],
23-
"curly": 0,
24-
"no-undef": 2,
25-
"quotes": [2, "single", "avoid-escape"],
26-
"no-trailing-spaces": 0,
27-
"no-underscore-dangle": 0,
28-
"no-multi-spaces": 0,
29-
"no-unused-expressions": 0,
30-
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
31-
"no-use-before-define": 0,
32-
"key-spacing": 0,
33-
"consistent-return": 0,
34-
"no-shadow": 0,
35-
"no-sequences": 0
36-
}
37-
}
1+
2+
parser: babel-eslint
3+
extends: jason

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ v0.14.1 - Tue, 16 Feb 2016 19:51:25 GMT
170170
v0.14.0 - Mon, 08 Feb 2016 16:17:40 GMT
171171
---------------------------------------
172172

173-
- [86b6446](../../commit/86b6446) [fixed] camelcase should maintain leading underscores
173+
- [86b6446](../../commit/86b6446) [fixed] camelCase should maintain leading underscores
174174

175175

176176

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ json separate from validating it, via the `cast` method.
8282
- [`object.shape(fields: object, noSortEdges: ?Array<[string, string]>): Schema`](#objectshapefields-object-nosortedges-arraystring-string-schema)
8383
- [`object.from(fromKey: string, toKey: string, alias: boolean = false): Schema`](#objectfromfromkey-string-tokey-string-alias-boolean--false-schema)
8484
- [`object.noUnknown(onlyKnownKeys: boolean = true, message: ?string): Schema`](#objectnounknownonlyknownkeys-boolean--true-message-string-schema)
85-
- [`object.camelcase(): Schema`](#objectcamelcase-schema)
86-
- [`object.constantcase(): Schema`](#objectconstantcase-schema)
85+
- [`object.camelCase(): Schema`](#objectcamelCase-schema)
86+
- [`object.constantCase(): Schema`](#objectconstantCase-schema)
8787
- [Extending Schema Types](#extending-schema-types)
8888

8989
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
@@ -697,8 +697,8 @@ Provide an arbitrary `regex` to match the value against.
697697
698698
```javascript
699699
var v = string().matches(/(hi|bye)/);
700-
v.isValid('hi').should.eventually.equal(true)
701-
v.isValid('nope').should.eventually.equal(false)
700+
v.isValid('hi').should.eventually().equal(true)
701+
v.isValid('nope').should.eventually().equal(false)
702702
```
703703
704704
#### `string.email(message: ?string): Schema`
@@ -935,11 +935,11 @@ inst.cast({ prop: 5, other: 6}) // => { myProp: 5, other: 6, Other: 6 }
935935
Validate that the object value only contains keys specified in `shape`, pass `false` as the first
936936
argument to disable the check. Restricting keys to known, also enables `stripUnknown` option, when not in strict mode.
937937
938-
#### `object.camelcase(): Schema`
938+
#### `object.camelCase(): Schema`
939939
940940
Transforms all object keys to camelCase
941941
942-
#### `object.constantcase(): Schema`
942+
#### `object.constantCase(): Schema`
943943
944944
Transforms all object keys to CONSTANT_CASE.
945945

benchmark/fixture.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var ChildAttribute = object({
6666

6767
seqID: number()
6868

69-
}).camelcase()
69+
}).camelCase()
7070

7171
var schema = object({
7272

@@ -93,11 +93,11 @@ var schema = object({
9393
valueID: number(),
9494
text: string()
9595
})
96-
.camelcase()
96+
.camelCase()
9797
),
9898

9999
children: array().of(ChildAttribute)
100-
}).camelcase()
100+
}).camelCase()
101101

102102

103103
module.exports = {

karma.conf.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ module.exports = function (config) {
66

77
basePath: '',
88

9-
frameworks: ['mocha'],
9+
frameworks: ['mocha', 'sinon-chai'],
1010

1111
reporters: ['mocha'],
1212

1313
files: [
14-
require.resolve('sinon/pkg/sinon-1.17.3.js'),
1514
'tests-webpack.js'
1615
],
1716

@@ -31,9 +30,6 @@ module.exports = function (config) {
3130
webpack: {
3231
devtool: 'inline-source-map',
3332
cache: true,
34-
resolve: {
35-
extensions: ['', '.js', '.jsx']
36-
},
3733
module: {
3834
loaders: [
3935
{

lib/Condition.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1414

1515
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
1616

17+
function callOrConcat(schema) {
18+
if (typeof schema === 'function') return schema;
19+
20+
return function (base) {
21+
return base.concat(schema);
22+
};
23+
}
24+
1725
var Conditional = function () {
1826
function Conditional(refs, options) {
1927
var _this = this;
@@ -27,6 +35,9 @@ var Conditional = function () {
2735

2836
this.refs = [].concat(refs);
2937

38+
then = callOrConcat(then);
39+
otherwise = callOrConcat(otherwise);
40+
3041
if (typeof options === 'function') this.fn = options;else {
3142
(function () {
3243
if (!(0, _has2.default)(options, 'is')) throw new TypeError('`is:` is required for `when()` conditions');
@@ -48,8 +59,10 @@ var Conditional = function () {
4859
values[_key2] = arguments[_key2];
4960
}
5061

51-
var ctx = values.pop();
52-
return isFn.apply(undefined, values) ? ctx.concat(then) : ctx.concat(otherwise);
62+
var currentSchema = values.pop();
63+
var option = isFn.apply(undefined, values) ? then : otherwise;
64+
65+
return option(currentSchema);
5366
};
5467
})();
5568
}

lib/array.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ exports.__esModule = true;
44

55
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
66

7+
var _templateObject = _taggedTemplateLiteralLoose(['', '[', ']'], ['', '[', ']']);
8+
79
var _typeName = require('type-name');
810

911
var _typeName2 = _interopRequireDefault(_typeName);
@@ -20,6 +22,10 @@ var _isSchema = require('./util/isSchema');
2022

2123
var _isSchema2 = _interopRequireDefault(_isSchema);
2224

25+
var _makePath = require('./util/makePath');
26+
27+
var _makePath2 = _interopRequireDefault(_makePath);
28+
2329
var _mixed = require('./mixed');
2430

2531
var _mixed2 = _interopRequireDefault(_mixed);
@@ -32,6 +38,8 @@ var _runValidations2 = _interopRequireDefault(_runValidations);
3238

3339
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3440

41+
function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; }
42+
3543
var hasLength = function hasLength(value) {
3644
return !(0, _isAbsent2.default)(value) && value.length > 0;
3745
};
@@ -99,7 +107,7 @@ function ArraySchema(type) {
99107
}
100108

101109
var validations = value.map(function (item, idx) {
102-
var path = (options.path || '') + '[' + idx + ']';
110+
var path = (0, _makePath2.default)(_templateObject, options.path, idx);
103111

104112
// object._validate note for isStrict explanation
105113
var innerOptions = _extends({}, options, {
@@ -122,22 +130,10 @@ function ArraySchema(type) {
122130
});
123131
});
124132
},
125-
126-
127-
// concat(schema) {
128-
// var next = MixedSchema.prototype.concat.call(this, schema)
129-
//
130-
// next._subType = schema._subType === undefined
131-
// ? this._subType
132-
// : schema._subType;
133-
//
134-
// return next
135-
// },
136-
137133
of: function of(schema) {
138134
var next = this.clone();
139135

140-
if (schema !== false && !(0, _isSchema2.default)(schema)) throw new TypeError('`array.of()` sub-schema must be a valid yup schema, or `false` to negate a current sub-schema. ' + 'got: ' + (0, _typeName2.default)(schema) + ' instead');
136+
if (schema !== false && !(0, _isSchema2.default)(schema)) throw new TypeError('`array.of()` sub-schema must be a valid yup schema, or `false` to negate a current sub-schema. ' + 'not: ' + (0, _typeName2.default)(schema));
141137

142138
next._subType = schema;
143139

lib/mixed.js

+21-31
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
44

5-
var _typeName = require('type-name');
6-
7-
var _typeName2 = _interopRequireDefault(_typeName);
8-
95
var _has = require('lodash/has');
106

117
var _has2 = _interopRequireDefault(_has);
@@ -160,24 +156,25 @@ SchemaType.prototype = {
160156
return this;
161157
},
162158
cast: function cast(value) {
163-
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
164-
165-
var schema = this.resolve(opts);
159+
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
166160

167-
var result = schema._cast(value, opts);
161+
var resolvedSchema = this.resolve(options);
162+
var result = resolvedSchema._cast(value, options);
168163

169-
if (opts.assert !== false && !this.isType(result)) {
170-
throw new TypeError('Expected ' + (opts.path || 'field') + ' to be type: "' + this._type + '". ' + ('Got "' + (0, _typeName2.default)(value) + '" instead.'));
164+
if (value !== undefined && options.assert !== false && resolvedSchema.isType(result) !== true) {
165+
var formattedValue = JSON.stringify(value);
166+
var formattedResult = JSON.stringify(result);
167+
throw new TypeError('The value of ' + (options.path || 'field') + ' could not be cast to a value ' + ('that satisfies the schema type: "' + resolvedSchema._type + '". \n\n') + ('attempted value: ' + JSON.stringify(value) + ' \n') + (formattedResult !== formattedValue ? 'result of cast: ' + JSON.stringify(result) : ''));
171168
}
172169

173170
return result;
174171
},
175-
_cast: function _cast(_value) {
172+
_cast: function _cast(rawValue) {
176173
var _this2 = this;
177174

178-
var value = _value === undefined ? _value : this.transforms.reduce(function (value, transform) {
179-
return transform.call(_this2, value, _value);
180-
}, _value);
175+
var value = rawValue === undefined ? rawValue : this.transforms.reduce(function (value, fn) {
176+
return fn.call(_this2, value, rawValue);
177+
}, rawValue);
181178

182179
if (value === undefined && (0, _has2.default)(this, '_default')) {
183180
value = this.default();
@@ -195,34 +192,28 @@ SchemaType.prototype = {
195192

196193
return nodeify(schema._validate(value, options), cb);
197194
},
198-
199-
200-
//-- tests
201195
_validate: function _validate(_value) {
202196
var _this3 = this;
203197

204198
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
205199

206-
var value = _value,
207-
schema = void 0,
208-
endEarly = void 0,
209-
isStrict = void 0;
200+
var value = _value;
210201

211-
schema = this;
212-
isStrict = this._option('strict', options);
213-
endEarly = this._option('abortEarly', options);
202+
var isStrict = this._option('strict', options);
203+
var endEarly = this._option('abortEarly', options);
214204

215205
var path = options.path;
216206
var label = this._label;
217207

218208
if (!isStrict) {
209+
219210
value = this._cast(value, _extends({ assert: false }, options));
220211
}
221212
// value is cast, we can check if it meets type requirements
222213
var validationParams = { value: value, path: path, schema: this, options: options, label: label };
223214
var initialTests = [];
224215

225-
if (schema._typeError) initialTests.push(this._typeError(validationParams));
216+
if (this._typeError) initialTests.push(this._typeError(validationParams));
226217

227218
if (this._whitelistError) initialTests.push(this._whitelistError(validationParams));
228219

@@ -258,8 +249,9 @@ SchemaType.prototype = {
258249
},
259250
default: function _default(def) {
260251
if (arguments.length === 0) {
261-
var dflt = (0, _has2.default)(this, '_default') ? this._default : this._defaultDefault;
262-
return typeof dflt === 'function' ? dflt.call(this) : (0, _clone2.default)(dflt);
252+
var defaultValue = (0, _has2.default)(this, '_default') ? this._default : this._defaultDefault;
253+
254+
return typeof defaultValue === 'function' ? defaultValue.call(this) : (0, _clone2.default)(defaultValue);
263255
}
264256

265257
var next = this.clone();
@@ -308,7 +300,7 @@ SchemaType.prototype = {
308300
var isExclusive = opts.exclusive || opts.name && next._exclusive[opts.name] === true;
309301

310302
if (opts.exclusive && !opts.name) {
311-
throw new TypeError('You cannot have an exclusive validation without a `name`');
303+
throw new TypeError('Exclusive tests must provide a unique `name` identifying the test');
312304
}
313305

314306
next._exclusive[opts.name] = !!opts.exclusive;
@@ -343,8 +335,8 @@ SchemaType.prototype = {
343335
var next = this.clone();
344336

345337
next._typeError = (0, _createValidation2.default)({
346-
message: message,
347338
name: 'typeError',
339+
message: message,
348340
test: function test(value) {
349341
if (value !== undefined && !this.schema.isType(value)) return this.createError({
350342
params: {
@@ -361,8 +353,6 @@ SchemaType.prototype = {
361353

362354
var next = this.clone();
363355

364-
if (next.tests.length) throw new TypeError('Cannot specify values when there are validation rules specified');
365-
366356
enums.forEach(function (val) {
367357
next._blacklist.delete(val);
368358
next._whitelist.add(val);

0 commit comments

Comments
 (0)