@znck/prop-types
Advanced tools
Comparing version 0.5.4 to 0.6.0
@@ -5,2 +5,9 @@ # Change Log | ||
<a name="0.6.0"></a> | ||
## 0.6.0 (2019-02-16) | ||
* feat: Add context logger to PropTypes.run helper, rename value to defaultValue and rename validate t ([381c253](https://github.com/znck/vue-prop-types/commit/381c253)) | ||
<a name="0.5.4"></a> | ||
@@ -7,0 +14,0 @@ ## <small>0.5.4 (2019-02-10)</small> |
/** | ||
* @znck/prop-types v0.5.4 | ||
* @znck/prop-types v0.6.0 | ||
* (c) 2019 Rahul Kadyan <hi@znck.me> (https://znck.me) | ||
@@ -43,2 +43,94 @@ * @license MIT | ||
let config = {}; | ||
try { | ||
config = require('vue'); | ||
} catch (e) {} | ||
const hasConsole = typeof console !== 'undefined'; | ||
const classifyRE = /(?:^|[-_])(\w)/g; | ||
const classify = str => str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, ''); | ||
const warn = (msg, vm) => { | ||
const trace = vm ? generateComponentTrace(vm) : ''; | ||
if (config.warnHandler) { | ||
config.warnHandler.call(null, msg, vm, trace); | ||
} else if (hasConsole && !config.silent) { | ||
console.error(`[Vue warn]: ${msg}${trace}`); | ||
} | ||
}; | ||
const error = (msg, vm) => { | ||
const trace = vm ? generateComponentTrace(vm) : ''; | ||
if (config.errorHandler) { | ||
config.errorHandler.call(null, msg, vm, trace); | ||
} else if (hasConsole && !config.silent) { | ||
console.error(`[Vue error]: ${msg}${trace}`); | ||
} | ||
}; | ||
const tip = (msg, vm) => { | ||
if (hasConsole && !config.silent) { | ||
console.warn(`[Vue tip]: ${msg}` + (vm ? generateComponentTrace(vm) : '')); | ||
} | ||
}; | ||
const formatComponentName = (vm, includeFile) => { | ||
if (vm.$root === vm) { | ||
return '<Root>'; | ||
} | ||
const options = typeof vm === 'function' && vm.cid != null ? vm.options : vm._isVue ? vm.$options || vm.constructor.options : vm; | ||
let name = options.name || options._componentTag; | ||
const file = options.__file; | ||
if (!name && file) { | ||
const match = file.match(/([^/\\]+)\.vue$/); | ||
name = match && match[1]; | ||
} | ||
return (name ? `<${classify(name)}>` : `<Anonymous>`) + (file && includeFile !== false ? ` at ${file}` : ''); | ||
}; | ||
const repeat = (str, n) => { | ||
let res = ''; | ||
while (n) { | ||
if (n % 2 === 1) res += str; | ||
if (n > 1) str += str; | ||
n >>= 1; | ||
} | ||
return res; | ||
}; | ||
const generateComponentTrace = vm => { | ||
if (vm._isVue && vm.$parent) { | ||
const tree = []; | ||
let currentRecursiveSequence = 0; | ||
while (vm) { | ||
if (tree.length > 0) { | ||
const last = tree[tree.length - 1]; | ||
if (last.constructor === vm.constructor) { | ||
currentRecursiveSequence++; | ||
vm = vm.$parent; | ||
continue; | ||
} else if (currentRecursiveSequence > 0) { | ||
tree[tree.length - 1] = [last, currentRecursiveSequence]; | ||
currentRecursiveSequence = 0; | ||
} | ||
} | ||
tree.push(vm); | ||
vm = vm.$parent; | ||
} | ||
return '\n\nfound in\n\n' + tree.map((vm, i) => `${i === 0 ? '---> ' : repeat(' ', 5 + i * 2)}${Array.isArray(vm) ? `${formatComponentName(vm[0])}... (${vm[1]} recursive calls)` : formatComponentName(vm)}`).join('\n'); | ||
} else { | ||
return `\n\n(found in ${formatComponentName(vm)})`; | ||
} | ||
}; | ||
/** @type {import('../types/index')} */ | ||
@@ -66,3 +158,8 @@ | ||
value(value) { | ||
this.default = value && typeof value === 'object' ? () => value : value; | ||
console.warn(`'PropType.[type].value' is deprecated. Use 'PropType.[type].defaultValue' instead.`); | ||
return this.defaultValue(value); | ||
} | ||
defaultValue(value) { | ||
this.default = value; | ||
return this; | ||
@@ -77,2 +174,7 @@ } | ||
validate(cb) { | ||
console.warn(`'PropType.[type].validate' is deprecated. Use 'PropType.[type].customValidator' instead.`); | ||
return this.customValidator(cb); | ||
} | ||
customValidator(cb) { | ||
var _this = this; | ||
@@ -113,35 +215,72 @@ | ||
static get string() { | ||
return this.create(String); | ||
const prop = this.create(String); | ||
prop.__meta__ = { | ||
custom: 'any' | ||
}; | ||
return prop; | ||
} | ||
static get number() { | ||
return this.create(Number); | ||
const prop = this.create(Number); | ||
prop.__meta__ = { | ||
custom: 'number' | ||
}; | ||
return prop; | ||
} | ||
static get bool() { | ||
return this.create(Boolean); | ||
const prop = this.create(Boolean); | ||
prop.__meta__ = { | ||
custom: 'bool' | ||
}; | ||
return prop; | ||
} | ||
static get array() { | ||
return this.create(Array); | ||
const prop = this.create(Array); | ||
prop.__meta__ = { | ||
custom: 'array' | ||
}; | ||
return prop; | ||
} | ||
static get object() { | ||
return this.create(Object); | ||
const prop = this.create(Object); | ||
prop.__meta__ = { | ||
custom: 'object' | ||
}; | ||
return prop; | ||
} | ||
static get func() { | ||
return this.create(Function); | ||
const prop = this.create(Function); | ||
prop.__meta__ = { | ||
custom: 'func' | ||
}; | ||
return prop; | ||
} | ||
static get symbol() { | ||
return this.create(Symbol); | ||
const prop = this.create(Symbol); | ||
prop.__meta__ = { | ||
custom: 'any' | ||
}; | ||
return prop; | ||
} | ||
static get any() { | ||
return this.create(); | ||
const prop = this.create(); | ||
prop.__meta__ = { | ||
custom: 'any' | ||
}; | ||
return prop; | ||
} | ||
static instanceOf(type) { | ||
return this.create(type); | ||
const prop = this.create(type); | ||
prop.__meta__ = { | ||
custom: 'instanceOf', | ||
type | ||
}; | ||
return prop; | ||
} | ||
@@ -159,2 +298,6 @@ | ||
const setOfValues = new Set(values); | ||
prop.__meta__ = { | ||
custom: 'oneOf', | ||
values | ||
}; | ||
@@ -176,2 +319,6 @@ prop.validator = value => { | ||
const prop = this.create(flat(types.map(type => ensureArray(type.type)))); | ||
prop.__meta__ = { | ||
custom: 'oneOfType', | ||
types | ||
}; | ||
@@ -196,2 +343,7 @@ prop.validator = value => types.some(validator => runValidation(validator, value)); | ||
const types = flat(expected).map(normalizeType); | ||
prop.__meta__ = { | ||
custom: 'collection', | ||
type, | ||
item: types | ||
}; | ||
@@ -225,8 +377,18 @@ prop.validator = value => (type === Array ? value : Object.values(value)).every(item => types.some(type => runValidation(type, item))); | ||
static validate(fn) { | ||
console.warn(`'PropType.validate' is deprecated. Use 'PropType.run' instead.`); | ||
return this.run(null, fn); | ||
} | ||
static run(context, fn) { | ||
if (arguments.length === 1) { | ||
fn = context; | ||
context = null; | ||
} | ||
const logger = createLogger(context); | ||
try { | ||
if (fn() === false) { | ||
console.error('There are some failing validation.'); | ||
} | ||
return fn(logger); | ||
} catch (e) { | ||
console.error(e); | ||
logger.error(e.message); | ||
} | ||
@@ -237,2 +399,10 @@ } | ||
function createLogger(context) { | ||
return { | ||
error: message => error(message, context), | ||
tip: message => tip(message, context), | ||
warn: message => warn(message, context) | ||
}; | ||
} | ||
function normalizeType(type) { | ||
@@ -239,0 +409,0 @@ if (type instanceof PropTypes) return type; |
/** | ||
* @znck/prop-types v0.5.4 | ||
* @znck/prop-types v0.6.0 | ||
* (c) 2019 Rahul Kadyan <hi@znck.me> (https://znck.me) | ||
@@ -45,2 +45,94 @@ * @license MIT | ||
let config = {}; | ||
try { | ||
config = require('vue'); | ||
} catch (e) {} | ||
const hasConsole = typeof console !== 'undefined'; | ||
const classifyRE = /(?:^|[-_])(\w)/g; | ||
const classify = str => str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, ''); | ||
const warn = (msg, vm) => { | ||
const trace = vm ? generateComponentTrace(vm) : ''; | ||
if (config.warnHandler) { | ||
config.warnHandler.call(null, msg, vm, trace); | ||
} else if (hasConsole && !config.silent) { | ||
console.error(`[Vue warn]: ${msg}${trace}`); | ||
} | ||
}; | ||
const error = (msg, vm) => { | ||
const trace = vm ? generateComponentTrace(vm) : ''; | ||
if (config.errorHandler) { | ||
config.errorHandler.call(null, msg, vm, trace); | ||
} else if (hasConsole && !config.silent) { | ||
console.error(`[Vue error]: ${msg}${trace}`); | ||
} | ||
}; | ||
const tip = (msg, vm) => { | ||
if (hasConsole && !config.silent) { | ||
console.warn(`[Vue tip]: ${msg}` + (vm ? generateComponentTrace(vm) : '')); | ||
} | ||
}; | ||
const formatComponentName = (vm, includeFile) => { | ||
if (vm.$root === vm) { | ||
return '<Root>'; | ||
} | ||
const options = typeof vm === 'function' && vm.cid != null ? vm.options : vm._isVue ? vm.$options || vm.constructor.options : vm; | ||
let name = options.name || options._componentTag; | ||
const file = options.__file; | ||
if (!name && file) { | ||
const match = file.match(/([^/\\]+)\.vue$/); | ||
name = match && match[1]; | ||
} | ||
return (name ? `<${classify(name)}>` : `<Anonymous>`) + (file && includeFile !== false ? ` at ${file}` : ''); | ||
}; | ||
const repeat = (str, n) => { | ||
let res = ''; | ||
while (n) { | ||
if (n % 2 === 1) res += str; | ||
if (n > 1) str += str; | ||
n >>= 1; | ||
} | ||
return res; | ||
}; | ||
const generateComponentTrace = vm => { | ||
if (vm._isVue && vm.$parent) { | ||
const tree = []; | ||
let currentRecursiveSequence = 0; | ||
while (vm) { | ||
if (tree.length > 0) { | ||
const last = tree[tree.length - 1]; | ||
if (last.constructor === vm.constructor) { | ||
currentRecursiveSequence++; | ||
vm = vm.$parent; | ||
continue; | ||
} else if (currentRecursiveSequence > 0) { | ||
tree[tree.length - 1] = [last, currentRecursiveSequence]; | ||
currentRecursiveSequence = 0; | ||
} | ||
} | ||
tree.push(vm); | ||
vm = vm.$parent; | ||
} | ||
return '\n\nfound in\n\n' + tree.map((vm, i) => `${i === 0 ? '---> ' : repeat(' ', 5 + i * 2)}${Array.isArray(vm) ? `${formatComponentName(vm[0])}... (${vm[1]} recursive calls)` : formatComponentName(vm)}`).join('\n'); | ||
} else { | ||
return `\n\n(found in ${formatComponentName(vm)})`; | ||
} | ||
}; | ||
/** @type {import('../types/index')} */ | ||
@@ -68,3 +160,8 @@ | ||
value(value) { | ||
this.default = value && typeof value === 'object' ? () => value : value; | ||
console.warn(`'PropType.[type].value' is deprecated. Use 'PropType.[type].defaultValue' instead.`); | ||
return this.defaultValue(value); | ||
} | ||
defaultValue(value) { | ||
this.default = value; | ||
return this; | ||
@@ -79,2 +176,7 @@ } | ||
validate(cb) { | ||
console.warn(`'PropType.[type].validate' is deprecated. Use 'PropType.[type].customValidator' instead.`); | ||
return this.customValidator(cb); | ||
} | ||
customValidator(cb) { | ||
var _this = this; | ||
@@ -115,35 +217,72 @@ | ||
static get string() { | ||
return this.create(String); | ||
const prop = this.create(String); | ||
prop.__meta__ = { | ||
custom: 'any' | ||
}; | ||
return prop; | ||
} | ||
static get number() { | ||
return this.create(Number); | ||
const prop = this.create(Number); | ||
prop.__meta__ = { | ||
custom: 'number' | ||
}; | ||
return prop; | ||
} | ||
static get bool() { | ||
return this.create(Boolean); | ||
const prop = this.create(Boolean); | ||
prop.__meta__ = { | ||
custom: 'bool' | ||
}; | ||
return prop; | ||
} | ||
static get array() { | ||
return this.create(Array); | ||
const prop = this.create(Array); | ||
prop.__meta__ = { | ||
custom: 'array' | ||
}; | ||
return prop; | ||
} | ||
static get object() { | ||
return this.create(Object); | ||
const prop = this.create(Object); | ||
prop.__meta__ = { | ||
custom: 'object' | ||
}; | ||
return prop; | ||
} | ||
static get func() { | ||
return this.create(Function); | ||
const prop = this.create(Function); | ||
prop.__meta__ = { | ||
custom: 'func' | ||
}; | ||
return prop; | ||
} | ||
static get symbol() { | ||
return this.create(Symbol); | ||
const prop = this.create(Symbol); | ||
prop.__meta__ = { | ||
custom: 'any' | ||
}; | ||
return prop; | ||
} | ||
static get any() { | ||
return this.create(); | ||
const prop = this.create(); | ||
prop.__meta__ = { | ||
custom: 'any' | ||
}; | ||
return prop; | ||
} | ||
static instanceOf(type) { | ||
return this.create(type); | ||
const prop = this.create(type); | ||
prop.__meta__ = { | ||
custom: 'instanceOf', | ||
type | ||
}; | ||
return prop; | ||
} | ||
@@ -161,2 +300,6 @@ | ||
const setOfValues = new Set(values); | ||
prop.__meta__ = { | ||
custom: 'oneOf', | ||
values | ||
}; | ||
@@ -178,2 +321,6 @@ prop.validator = value => { | ||
const prop = this.create(flat(types.map(type => ensureArray(type.type)))); | ||
prop.__meta__ = { | ||
custom: 'oneOfType', | ||
types | ||
}; | ||
@@ -198,2 +345,7 @@ prop.validator = value => types.some(validator => runValidation(validator, value)); | ||
const types = flat(expected).map(normalizeType); | ||
prop.__meta__ = { | ||
custom: 'collection', | ||
type, | ||
item: types | ||
}; | ||
@@ -227,8 +379,18 @@ prop.validator = value => (type === Array ? value : Object.values(value)).every(item => types.some(type => runValidation(type, item))); | ||
static validate(fn) { | ||
console.warn(`'PropType.validate' is deprecated. Use 'PropType.run' instead.`); | ||
return this.run(null, fn); | ||
} | ||
static run(context, fn) { | ||
if (arguments.length === 1) { | ||
fn = context; | ||
context = null; | ||
} | ||
const logger = createLogger(context); | ||
try { | ||
if (fn() === false) { | ||
console.error('There are some failing validation.'); | ||
} | ||
return fn(logger); | ||
} catch (e) { | ||
console.error(e); | ||
logger.error(e.message); | ||
} | ||
@@ -239,2 +401,10 @@ } | ||
function createLogger(context) { | ||
return { | ||
error: message => error(message, context), | ||
tip: message => tip(message, context), | ||
warn: message => warn(message, context) | ||
}; | ||
} | ||
function normalizeType(type) { | ||
@@ -241,0 +411,0 @@ if (type instanceof PropTypes) return type; |
{ | ||
"name": "@znck/prop-types", | ||
"version": "0.5.4", | ||
"version": "0.6.0", | ||
"description": "Fluent prop validation for Vue", | ||
@@ -55,3 +55,8 @@ "main": "dist/prop-types.js", | ||
}, | ||
"sideEffects": false | ||
"sideEffects": false, | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
} | ||
} |
/** | ||
* @znck/prop-types v0.5.4 | ||
* @znck/prop-types v0.6.0 | ||
* (c) 2019 Rahul Kadyan <hi@znck.me> (https://znck.me) | ||
@@ -4,0 +4,0 @@ * @license MIT |
141
src/index.js
@@ -7,3 +7,3 @@ import { | ||
typeValues, | ||
flat | ||
flat, | ||
} from './helpers' | ||
@@ -32,4 +32,10 @@ | ||
value(value) { | ||
this.default = value && typeof value === 'object' ? () => value : value | ||
console.warn(`'PropType.[type].value' is deprecated. Use 'PropType.[type].defaultValue' instead.`) | ||
return this.defaultValue(value) | ||
} | ||
defaultValue(value) { | ||
this.default = value | ||
return this | ||
@@ -45,2 +51,8 @@ } | ||
validate(cb) { | ||
console.warn(`'PropType.[type].validate' is deprecated. Use 'PropType.[type].customValidator' instead.`) | ||
return this.customValidator(cb) | ||
} | ||
customValidator(cb) { | ||
if (!(typeof cb === 'function')) return this | ||
@@ -79,35 +91,90 @@ | ||
static get string() { | ||
return this.create(String) | ||
const prop = this.create(String) | ||
prop.__meta__ = { | ||
custom: 'any', | ||
} | ||
return prop | ||
} | ||
static get number() { | ||
return this.create(Number) | ||
const prop = this.create(Number) | ||
prop.__meta__ = { | ||
custom: 'number', | ||
} | ||
return prop | ||
} | ||
static get bool() { | ||
return this.create(Boolean) | ||
const prop = this.create(Boolean) | ||
prop.__meta__ = { | ||
custom: 'bool', | ||
} | ||
return prop | ||
} | ||
static get array() { | ||
return this.create(Array) | ||
const prop = this.create(Array) | ||
prop.__meta__ = { | ||
custom: 'array', | ||
} | ||
return prop | ||
} | ||
static get object() { | ||
return this.create(Object) | ||
const prop = this.create(Object) | ||
prop.__meta__ = { | ||
custom: 'object', | ||
} | ||
return prop | ||
} | ||
static get func() { | ||
return this.create(Function) | ||
const prop = this.create(Function) | ||
prop.__meta__ = { | ||
custom: 'func', | ||
} | ||
return prop | ||
} | ||
static get symbol() { | ||
return this.create(Symbol) | ||
const prop = this.create(Symbol) | ||
prop.__meta__ = { | ||
custom: 'any', | ||
} | ||
return prop | ||
} | ||
static get any() { | ||
return this.create() | ||
const prop = this.create() | ||
prop.__meta__ = { | ||
custom: 'any', | ||
} | ||
return prop | ||
} | ||
static instanceOf(type) { | ||
return this.create(type) | ||
const prop = this.create(type) | ||
prop.__meta__ = { | ||
custom: 'instanceOf', | ||
type | ||
} | ||
return prop | ||
} | ||
@@ -120,6 +187,13 @@ | ||
const types = Array.from(new Set(values.map(value => TYPES[typeof value] || Object))) | ||
const types = Array.from( | ||
new Set(values.map(value => TYPES[typeof value] || Object)) | ||
) | ||
const prop = this.create(types) | ||
const setOfValues = new Set(values) | ||
prop.__meta__ = { | ||
custom: 'oneOf', | ||
values | ||
} | ||
prop.validator = value => { | ||
@@ -139,2 +213,7 @@ return setOfValues.has(value) | ||
prop.__meta__ = { | ||
custom: 'oneOfType', | ||
types, | ||
} | ||
prop.validator = value => | ||
@@ -159,2 +238,8 @@ types.some(validator => runValidation(validator, value)) | ||
prop.__meta__ = { | ||
custom: 'collection', | ||
type, | ||
item: types, | ||
} | ||
prop.validator = value => | ||
@@ -172,3 +257,3 @@ (type === Array ? value : Object.values(value)).every(item => | ||
Object.entries(shape).forEach(({0: key, 1: value}) => { | ||
Object.entries(shape).forEach(({ 0: key, 1: value }) => { | ||
shapeType[key] = normalizeType(value) | ||
@@ -180,3 +265,3 @@ }) | ||
return Object.entries(shapeType).every(({0: key, 1: type}) => | ||
return Object.entries(shapeType).every(({ 0: key, 1: type }) => | ||
runValidation(type, value[key]) | ||
@@ -190,8 +275,19 @@ ) | ||
static validate(fn) { | ||
console.warn(`'PropType.validate' is deprecated. Use 'PropType.run' instead.`) | ||
return this.run(null, fn) | ||
} | ||
static run(context, fn) { | ||
if (arguments.length === 1) { | ||
fn = context | ||
context = null | ||
} | ||
const logger = createLogger(context) | ||
try { | ||
if (fn() === false) { | ||
console.error('There are some failing validation.') | ||
} | ||
return fn(logger) | ||
} catch (e) { | ||
console.error(e) | ||
logger.error(e.message) | ||
} | ||
@@ -201,2 +297,11 @@ } | ||
import * as logger from './logger' | ||
function createLogger(context) { | ||
return { | ||
error: message => logger.error(message, context), | ||
tip: message => logger.tip(message, context), | ||
warn: message => logger.warn(message, context), | ||
} | ||
} | ||
function normalizeType(type) { | ||
@@ -203,0 +308,0 @@ if (type instanceof PropTypes) return type |
@@ -5,3 +5,3 @@ // Type definitions for @znck/prop-types | ||
import { PropOptions } from 'vue' | ||
import Vue, { PropOptions } from 'vue' | ||
import { Prop } from 'vue/types/options' | ||
@@ -86,5 +86,13 @@ | ||
validate(fn: () => void): void | ||
validate(fn: (logger: ContextLogger) => void): void | ||
run<R = any>(fn: (logger: ContextLogger) => R): R | ||
run<R = any>(context: Vue, fn: (logger: ContextLogger) => R): R | ||
} | ||
export interface ContextLogger { | ||
error(message: string): void | ||
tip(message: string): void | ||
warn(message: string): void | ||
} | ||
export interface PropTypesChain< | ||
@@ -112,3 +120,5 @@ T, | ||
value(value: U): PropValidator<T> | ||
defaultValue(value: U): PropValidator<T> | ||
validate: (value: ValidatorFn<T>) => PropValidator<T> | ||
customValidator: (value: ValidatorFn<T>) => PropValidator<T> | ||
description: (value: string) => PropValidator<T> & { description: string } | ||
@@ -115,0 +125,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52644
12
1410