Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Feb 9, 2018
1 parent a4d724a commit a25262b
Show file tree
Hide file tree
Showing 40 changed files with 3,071 additions and 9,091 deletions.
6,792 changes: 0 additions & 6,792 deletions package-lock.json

This file was deleted.

27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
"scripts": {
"test": "npm run lint && npm run test-all -- --runInBand",
"testonly": "jest",
"test-all": "npm run testonly -- --projects ./jest-sync.config.js --projects ./package.json",
"test-all":
"npm run testonly -- --projects ./jest-sync.config.js --projects ./package.json",
"tdd": "jest --watch",
"lint": "eslint src test",
"clean": "rimaf ./lib/*",
"precommit": "lint-staged",
"toc": "doctoc README.md --github",
"build": "babel src --out-dir lib && npm run toc",
"release": "release"
},
"files": [
"lib"
],
"files": ["lib"],
"repository": {
"type": "git",
"url": "https://github.com/jquense/yup.git"
Expand All @@ -27,16 +26,19 @@
"url": "https://github.com/jquense/yup/issues"
},
"homepage": "https://github.com/jquense/yup",
"prettier": {
"singleQuote": true,
"trailingComma": "all"
},
"lint-staged": {
"*.{js,json,css,md}": ["prettier --write", "git add"]
},
"jest": {
"testEnvironment": "node",
"setupTestFrameworkScriptFile": "./test-setup.js",
"roots": [
"test"
],
"roots": ["test"],
"testRegex": "\\.js",
"testPathIgnorePatterns": [
"helpers\\.js"
]
"testPathIgnorePatterns": ["helpers\\.js"]
},
"devDependencies": {
"babel-cli": "^6.6.5",
Expand All @@ -53,8 +55,11 @@
"eslint-config-jason": "^4.0.0",
"eslint-plugin-import": "^1.14.0",
"eslint-plugin-react": "^6.2.0",
"husky": "^0.14.3",
"jest": "^20.0.3",
"lint-staged": "^6.1.0",
"mt-changelog": "^0.6.2",
"prettier": "^1.10.2",
"promises-aplus-tests": "^2.1.2",
"release-script": "^0.5.2",
"sinon": "^1.17.7",
Expand Down
44 changes: 21 additions & 23 deletions src/Condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,57 @@ import has from 'lodash/has';
import isSchema from './util/isSchema';

function callOrConcat(schema) {
if (typeof schema === 'function')
return schema
if (typeof schema === 'function') return schema;

return base => base.concat(schema)
return base => base.concat(schema);
}

class Conditional {

constructor(refs, options) {
let { is, then, otherwise } = options;

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

then = callOrConcat(then);
otherwise = callOrConcat(otherwise);

if (typeof options === 'function')
this.fn = options
else
{
if (typeof options === 'function') this.fn = options;
else {
if (!has(options, 'is'))
throw new TypeError('`is:` is required for `when()` conditions')
throw new TypeError('`is:` is required for `when()` conditions');

if (!options.then && !options.otherwise)
throw new TypeError(
'either `then:` or `otherwise:` is required for `when()` conditions'
)
'either `then:` or `otherwise:` is required for `when()` conditions',
);

let isFn = typeof is === 'function'
? is : ((...values) => values.every(value => value === is))
let isFn =
typeof is === 'function'
? is
: (...values) => values.every(value => value === is);

this.fn = function (...values) {
this.fn = function(...values) {
let currentSchema = values.pop();
let option = isFn(...values) ? then : otherwise
let option = isFn(...values) ? then : otherwise;

return option(currentSchema)
}
return option(currentSchema);
};
}
}

getValue(parent, context) {
let values = this.refs.map(r => r.getValue(parent, context))
let values = this.refs.map(r => r.getValue(parent, context));

return values
return values;
}

resolve(ctx, values) {
let schema = this.fn.apply(ctx, values.concat(ctx))
let schema = this.fn.apply(ctx, values.concat(ctx));

if (schema !== undefined && !isSchema(schema))
throw new TypeError('conditions must return a schema object')
throw new TypeError('conditions must return a schema object');

return schema || ctx
return schema || ctx;
}
}

Expand Down
20 changes: 9 additions & 11 deletions src/Lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@ import isSchema from './util/isSchema';

class Lazy {
constructor(mapFn) {
this._resolve = (...args) => {
let schema = mapFn(...args)
this._resolve = (...args) => {
let schema = mapFn(...args);
if (!isSchema(schema))
throw new TypeError('lazy() functions must return a valid schema')
throw new TypeError('lazy() functions must return a valid schema');

return schema
}
return schema;
};
}

resolve({ value, ...rest }) {
return this._resolve(value, rest)
return this._resolve(value, rest);
}

cast(value, options) {
return this._resolve(value, options)
.cast(value, options)
return this._resolve(value, options).cast(value, options);
}

validate(value, options) {
return this._resolve(value, options)
.validate(value, options)
return this._resolve(value, options).validate(value, options);
}
}

Lazy.prototype.__isYupSchema__ = true;

export default Lazy
export default Lazy;
33 changes: 17 additions & 16 deletions src/Reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,48 @@ import { getter } from 'property-expr';

let validateName = d => {
if (typeof d !== 'string')
throw new TypeError('ref\'s must be strings, got: ' + d)
}
throw new TypeError("ref's must be strings, got: " + d);
};

export default class Reference {
static isRef(value) {
return !!(value && (value.__isYupRef || value instanceof Reference))
return !!(value && (value.__isYupRef || value instanceof Reference));
}

toString() {
return `Ref(${this.key})`
return `Ref(${this.key})`;
}

constructor(key, mapFn, options = {}) {
validateName(key)
validateName(key);
let prefix = options.contextPrefix || '$';

if (typeof key === 'function') {
key = '.';

}

this.key = key.trim();
this.prefix = prefix;
this.isContext = this.key.indexOf(prefix) === 0
this.isSelf = this.key === '.';
this.isContext = this.key.indexOf(prefix) === 0;
this.isSelf = this.key === '.';

this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key
this._get = getter(this.path, true)
this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key;
this._get = getter(this.path, true);
this.map = mapFn || (value => value);
}
resolve() { return this; }
resolve() {
return this;
}

cast(value, { parent, context }) {
return this.getValue(parent, context)
return this.getValue(parent, context);
}

getValue(parent, context) {
let isContext = this.isContext
let value = this._get(isContext ? context : (parent || context) || {})
return this.map(value)
let isContext = this.isContext;
let value = this._get(isContext ? context : parent || context || {});
return this.map(value);
}
}

Reference.prototype.__isYupRef = true
Reference.prototype.__isYupRef = true;
53 changes: 25 additions & 28 deletions src/ValidationError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,47 @@ import printValue from './util/printValue';

let strReg = /\$\{\s*(\w+)\s*\}/g;

let replace = str =>
params => str.replace(strReg, (_, key) => printValue(params[key]))

let replace = str => params =>
str.replace(strReg, (_, key) => printValue(params[key]));

export default function ValidationError(errors, value, field, type) {
this.name = 'ValidationError'
this.value = value
this.path = field
this.type = type
this.errors = []
this.inner = []
this.name = 'ValidationError';
this.value = value;
this.path = field;
this.type = type;
this.errors = [];
this.inner = [];

if (errors)
[].concat(errors).forEach(err => {
this.errors = this.errors.concat(err.errors || err)
this.errors = this.errors.concat(err.errors || err);

if (err.inner)
this.inner = this.inner.concat(err.inner.length ? err.inner : err)
})
this.inner = this.inner.concat(err.inner.length ? err.inner : err);
});

this.message = this.errors.length > 1
? `${this.errors.length} errors occurred`
: this.errors[0]
this.message =
this.errors.length > 1
? `${this.errors.length} errors occurred`
: this.errors[0];

if (Error.captureStackTrace)
Error.captureStackTrace(this, ValidationError);
if (Error.captureStackTrace) Error.captureStackTrace(this, ValidationError);
}

ValidationError.prototype = Object.create(Error.prototype);
ValidationError.prototype.constructor = ValidationError;

ValidationError.isError = function(err){
return err && err.name === 'ValidationError'
}
ValidationError.isError = function(err) {
return err && err.name === 'ValidationError';
};

ValidationError.formatError = function(message, params) {

if (typeof message === 'string')
message = replace(message)
if (typeof message === 'string') message = replace(message);

let fn = ({ path, label, ...params }) => {
params.path = label || path || 'this'
return typeof message === 'function' ? message(params) : message
}
params.path = label || path || 'this';
return typeof message === 'function' ? message(params) : message;
};

return arguments.length === 1 ? fn : fn(params)
}
return arguments.length === 1 ? fn : fn(params);
};
Loading

0 comments on commit a25262b

Please sign in to comment.