typedconverter
Advanced tools
Comparing version 1.0.7 to 2.0.0
@@ -83,5 +83,5 @@ import validatorJs from "validator"; | ||
}): (target: any, name: string, index?: any) => void; | ||
function optional(): (target: any, name: string, index?: any) => void; | ||
function required(): (target: any, name: string, index?: any) => void; | ||
function partial(typ: Class): (...args: any[]) => void; | ||
} | ||
export { val, Opt }; |
@@ -203,6 +203,6 @@ "use strict"; | ||
val.whiteListed = whiteListed; | ||
function optional() { | ||
return v.optional(); | ||
function required() { | ||
return v.required(); | ||
} | ||
val.optional = optional; | ||
val.required = required; | ||
function partial(typ) { | ||
@@ -209,0 +209,0 @@ return v.partial(typ); |
import { Opt, val } from "./decorators"; | ||
import { VisitorExtension, VisitorInvocation } from "./invocation"; | ||
import { Class } from "./types"; | ||
import { optional, OptionalValidator, partial, PartialValidator, requiredValidationVisitor, Validator, validatorVisitor, ValidatorDecorator } from "./validation"; | ||
import { partial, PartialValidator, Validator, validatorVisitor, ValidatorDecorator } from "./validation"; | ||
import { Result, ResultMessages } from "./visitor"; | ||
@@ -40,2 +40,2 @@ interface Option { | ||
declare function validate(value: any, opt: Option): Result; | ||
export { convert, Option, VisitorExtension, VisitorInvocation, Result, ResultMessages, optional, partial, requiredValidationVisitor, validatorVisitor, Validator, OptionalValidator, PartialValidator, val, Opt, validate, ValidatorDecorator }; | ||
export { convert, Option, VisitorExtension, VisitorInvocation, Result, ResultMessages, partial, validatorVisitor, Validator, PartialValidator, val, Opt, validate, ValidatorDecorator }; |
@@ -7,7 +7,4 @@ "use strict"; | ||
const validation_1 = require("./validation"); | ||
exports.optional = validation_1.optional; | ||
exports.OptionalValidator = validation_1.OptionalValidator; | ||
exports.partial = validation_1.partial; | ||
exports.PartialValidator = validation_1.PartialValidator; | ||
exports.requiredValidationVisitor = validation_1.requiredValidationVisitor; | ||
exports.validatorVisitor = validation_1.validatorVisitor; | ||
@@ -38,3 +35,3 @@ const visitor_1 = require("./visitor"); | ||
function validate(value, opt) { | ||
const visitors = [validation_1.requiredValidationVisitor, validation_1.validatorVisitor]; | ||
const visitors = [validation_1.validatorVisitor]; | ||
for (const visitor of (opt.visitors || [])) { | ||
@@ -41,0 +38,0 @@ visitors.push(visitor); |
import { VisitorInvocation } from './invocation'; | ||
import { Result } from './visitor'; | ||
import { Class } from './types'; | ||
declare const OptionalValidator: unique symbol; | ||
declare const RequiredValidator: unique symbol; | ||
declare const PartialValidator: unique symbol; | ||
@@ -12,10 +12,5 @@ declare type Validator = (val: any) => string | undefined; | ||
declare function createValidation(validator: Validator | string | symbol): (target: any, name: string, index?: any) => void; | ||
declare function optional(): (target: any, name: string, index?: any) => void; | ||
declare function required(): (target: any, name: string, index?: any) => void; | ||
declare function partial(type: Class): (...args: any[]) => void; | ||
/** | ||
* When registered to the converter, this visitor will make all variable or properties required by default, except optional decorator added | ||
* @param i Visitor invocation | ||
*/ | ||
declare function requiredValidationVisitor(i: VisitorInvocation): Result; | ||
declare function validatorVisitor(i: VisitorInvocation): Result; | ||
export { createValidation, optional, partial, requiredValidationVisitor, validatorVisitor, Validator, ValidatorDecorator, OptionalValidator, PartialValidator }; | ||
export { createValidation, required, partial, validatorVisitor, Validator, ValidatorDecorator, PartialValidator, RequiredValidator }; |
@@ -7,4 +7,4 @@ "use strict"; | ||
const converter_1 = require("./converter"); | ||
const OptionalValidator = Symbol("tc:optional"); | ||
exports.OptionalValidator = OptionalValidator; | ||
const RequiredValidator = Symbol("tc:required"); | ||
exports.RequiredValidator = RequiredValidator; | ||
const PartialValidator = Symbol("tc:partial"); | ||
@@ -19,6 +19,6 @@ exports.PartialValidator = PartialValidator; | ||
exports.createValidation = createValidation; | ||
function optional() { | ||
return createValidation(OptionalValidator); | ||
function required() { | ||
return createValidation(RequiredValidator); | ||
} | ||
exports.optional = optional; | ||
exports.required = required; | ||
function partial(type) { | ||
@@ -31,5 +31,2 @@ return tinspector_1.mergeDecorator(tinspector_1.default.type(type, "Partial"), createValidation(PartialValidator)); | ||
// --------------------------------------------------------------------- // | ||
function isOptional(i) { | ||
return i.decorators.find((x) => x.validator === OptionalValidator); | ||
} | ||
function isPartial(i) { | ||
@@ -44,18 +41,5 @@ return i.parent && i.parent.decorators.find((x) => x.validator === PartialValidator); | ||
// --------------------------------------------------------------------- // | ||
/** | ||
* When registered to the converter, this visitor will make all variable or properties required by default, except optional decorator added | ||
* @param i Visitor invocation | ||
*/ | ||
function requiredValidationVisitor(i) { | ||
const result = i.proceed(); | ||
if (isEmpty(result) && !isOptional(i) && !isPartial(i)) | ||
return visitor_1.Result.error(i.value, i.path, `Required`); | ||
else | ||
return result; | ||
} | ||
exports.requiredValidationVisitor = requiredValidationVisitor; | ||
function getValidators(i) { | ||
return i.decorators | ||
.filter((x) => x.validator !== OptionalValidator | ||
&& x.validator !== PartialValidator && x.type === "tc:validator"); | ||
.filter((x) => x.validator !== PartialValidator && x.type === "tc:validator"); | ||
} | ||
@@ -65,3 +49,4 @@ function validatorVisitor(i) { | ||
const result = i.proceed(); | ||
//if empty then don't do any more validation | ||
if (!isPartial(i) && isEmpty(result) && validators.some(x => x.validator === RequiredValidator)) | ||
return visitor_1.Result.error(i.value, i.path, "Required"); | ||
if (isEmpty(result) || validators.length === 0) | ||
@@ -68,0 +53,0 @@ return result; |
{ | ||
"name": "typedconverter", | ||
"version": "1.0.7", | ||
"version": "2.0.0", | ||
"description": "Convert object into classes match with TypeScript type annotation", | ||
@@ -27,3 +27,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@types/ejs": "^2.7.0", | ||
"@types/ejs": "^3.0.0", | ||
"@types/jest": "^24.0.23", | ||
@@ -37,3 +37,3 @@ "@types/joi": "^14.3.4", | ||
"ts-node": "^8.5.4", | ||
"typescript": "^3.7.2" | ||
"typescript": "^3.7.3" | ||
}, | ||
@@ -40,0 +40,0 @@ "bugs": { |
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
38747
793