koa-router-meta
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -77,5 +77,5 @@ /** | ||
<h2>Sub Routes:</h2> | ||
${this._childRoutes ? this._childRoutes.map(v => renderSubRoute(currPath, v)) : ''} | ||
${this._childRoutes ? this._childRoutes.map(v => renderSubRoute(currPath, v)).join('') : ''} | ||
<h2>Requests:</h2> | ||
${this._requests ? this._requests.map(v => renderRequest(currPath, v)) : ''} | ||
${this._requests ? this._requests.map(v => renderRequest(currPath, v)).join('') : ''} | ||
</body> | ||
@@ -82,0 +82,0 @@ </html> |
{ | ||
"name": "koa-router-meta", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
202
types.js
@@ -8,54 +8,54 @@ /** | ||
function createValidatorFactory(func) { | ||
return (...args) => { | ||
const origin = func( ...args); | ||
return (...args) => { | ||
const origin = func( ...args); | ||
const ret = (values, name) => { | ||
if (values === undefined) { | ||
return undefined; | ||
} | ||
return origin(values, name); | ||
const ret = (values, name) => { | ||
if (values === undefined) { | ||
return undefined; | ||
} | ||
return origin(values, name); | ||
}; | ||
ret.json = () => origin.json(false) | ||
ret.html = () => origin.html(false); | ||
ret.required = (values, name) => { | ||
if (values === undefined) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} is required but missing.`); | ||
} | ||
return origin(values, name); | ||
}; | ||
ret.required.json = () => origin.json(true); | ||
ret.required.html = () => origin.html(true); | ||
return ret; | ||
}; | ||
ret.json = () => origin.json(false) | ||
ret.html = () => origin.html(false); | ||
ret.required = (values, name) => { | ||
if (values === undefined) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} is required but missing.`); | ||
} | ||
return origin(values, name); | ||
}; | ||
ret.required.json = () => origin.json(true); | ||
ret.required.html = () => origin.html(true); | ||
return ret; | ||
}; | ||
} | ||
function shapeValidator(comment, shape) { | ||
const fields = Object.keys(shape); | ||
const ret = (values, name) => { | ||
const result = {}; | ||
if (typeof values !== 'object' || values === null) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a object, but JSON.stringify(${value})`); | ||
} | ||
for (const field of fields) { | ||
result[field] = shape[field](values[field], __DEV__ && `${name}.${field}`); | ||
} | ||
return result; | ||
}; | ||
const fields = Object.keys(shape); | ||
const ret = (values, name) => { | ||
const result = {}; | ||
if (typeof values !== 'object' || values === null) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a object, but JSON.stringify(${value})`); | ||
} | ||
for (const field of fields) { | ||
result[field] = shape[field](values[field], __DEV__ && `${name}.${field}`); | ||
} | ||
return result; | ||
}; | ||
ret.json = (required) => { | ||
const ret = {}; | ||
ret.json = (required) => { | ||
const ret = {}; | ||
for (const field of fields) { | ||
ret[field] = shape[field].json(); | ||
} | ||
for (const field of fields) { | ||
ret[field] = shape[field].json(); | ||
} | ||
return ({ | ||
type: 'shape', | ||
required, | ||
comment, | ||
fields: ret, | ||
}) | ||
}; | ||
return ({ | ||
type: 'shape', | ||
required, | ||
comment, | ||
fields: ret, | ||
}) | ||
}; | ||
ret.html = (required) => ` | ||
ret.html = (required) => ` | ||
<p>${comment} ${required ? '(*)' : ''}</p> | ||
@@ -67,27 +67,27 @@ <table> | ||
<tbody> | ||
${fields.map(v=>`<td>${v}</td><td>${shape[v].html()}</td>`)} | ||
${fields.map(v=>`<tr><td>${v}</td><td>${shape[v].html()}</td></tr>`).join('')} | ||
</tbody> | ||
</table>`; | ||
return ret; | ||
return ret; | ||
} | ||
function arrayValidator(comment, type) { | ||
const ret = (values, name) => { | ||
if (!Array.isArray(values)) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a array, but JSON.stringify(${value})`); | ||
} | ||
return values.map((v, i) => type(v, __DEV__ && `${name}[${i}]`)); | ||
}; | ||
const ret = (values, name) => { | ||
if (!Array.isArray(values)) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a array, but JSON.stringify(${value})`); | ||
} | ||
return values.map((v, i) => type(v, __DEV__ && `${name}[${i}]`)); | ||
}; | ||
ret.json = (required) => { | ||
return ({ | ||
type: 'array', | ||
required, | ||
comment, | ||
elementType: type.json(), | ||
}) | ||
}; | ||
ret.json = (required) => { | ||
return ({ | ||
type: 'array', | ||
required, | ||
comment, | ||
elementType: type.json(), | ||
}) | ||
}; | ||
ret.html = (required) => ` | ||
ret.html = (required) => ` | ||
<p>${comment} ${required ? '(*)' : ''} Array of type:</p> | ||
@@ -97,59 +97,59 @@ ${type.html()} | ||
return ret; | ||
return ret; | ||
} | ||
function integerValidator(comment) { | ||
const ret = (value, name) => { | ||
if (typeof(value) === 'string') { | ||
try { | ||
return parseInt(value); | ||
} catch (e) { | ||
// throw exception later. | ||
} | ||
} else if (typeof(value) === 'number') { | ||
return value | 0; | ||
} | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a integer, but JSON.stringify(${value})`); | ||
}; | ||
const ret = (value, name) => { | ||
if (typeof(value) === 'string') { | ||
try { | ||
return parseInt(value); | ||
} catch (e) { | ||
// throw exception later. | ||
} | ||
} else if (typeof(value) === 'number') { | ||
return value | 0; | ||
} | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a integer, but JSON.stringify(${value})`); | ||
}; | ||
ret.json = (required) => { | ||
return ({ | ||
type: 'integer', | ||
required, | ||
comment, | ||
}) | ||
}; | ||
ret.json = (required) => { | ||
return ({ | ||
type: 'integer', | ||
required, | ||
comment, | ||
}) | ||
}; | ||
ret.html = (required) => ` | ||
ret.html = (required) => ` | ||
<p>${comment} ${required ? '(*)' : ''} Integer</p> | ||
`; | ||
return ret; | ||
return ret; | ||
} | ||
function regexpValidator(comment, reg) { | ||
const ret = (value, name) => { | ||
if (typeof(value) === 'string') { | ||
if (!reg.test(value)) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should match ${reg.toString()}, but JSON.stringify(${value})`); | ||
} | ||
return value; | ||
} | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a string, but JSON.stringify(${value})`); | ||
}; | ||
const ret = (value, name) => { | ||
if (typeof(value) === 'string') { | ||
if (!reg.test(value)) { | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should match ${reg.toString()}, but JSON.stringify(${value})`); | ||
} | ||
return value; | ||
} | ||
throw StatusError.BAD_REQUEST(__DEV__ && `Field ${name} should be a string, but JSON.stringify(${value})`); | ||
}; | ||
ret.json = (required) => { | ||
return ({ | ||
type: 'string', | ||
regexp: reg.toString(), | ||
required, | ||
comment, | ||
}); | ||
}; | ||
ret.json = (required) => { | ||
return ({ | ||
type: 'string', | ||
regexp: reg.toString(), | ||
required, | ||
comment, | ||
}); | ||
}; | ||
ret.html = (required) => ` | ||
ret.html = (required) => ` | ||
<p>${comment} ${required ? '(*)' : ''} String</p> | ||
<p>RegExp: ${reg.toString()}</p> | ||
`; | ||
return ret; | ||
return ret; | ||
} | ||
@@ -156,0 +156,0 @@ |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
13075
10
1
31