typedconverter
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
@@ -0,4 +1,5 @@ | ||
import { Opt, val } from "./decorators"; | ||
import { VisitorExtension, VisitorInvocation } from "./invocation"; | ||
import { Class } from "./types"; | ||
import { optional, OptionalValidator, partial, PartialValidator, requiredValidationVisitor, validate, Validator, ValidatorDecorator } from "./validation"; | ||
import { optional, OptionalValidator, partial, PartialValidator, requiredValidationVisitor, Validator, validatorVisitor, ValidatorDecorator } from "./validation"; | ||
import { Result, ResultMessages } from "./visitor"; | ||
@@ -34,2 +35,3 @@ interface Option { | ||
export default function createConverter(option: Option): (value: any) => Result; | ||
export { convert, Option, VisitorExtension, VisitorInvocation, Result, ResultMessages, validate, optional, partial, requiredValidationVisitor, Validator, ValidatorDecorator, OptionalValidator, PartialValidator }; | ||
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 }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const decorators_1 = require("./decorators"); | ||
exports.val = decorators_1.val; | ||
const transformer_1 = require("./transformer"); | ||
@@ -10,3 +12,3 @@ const validation_1 = require("./validation"); | ||
exports.requiredValidationVisitor = validation_1.requiredValidationVisitor; | ||
exports.validate = validation_1.validate; | ||
exports.validatorVisitor = validation_1.validatorVisitor; | ||
const visitor_1 = require("./visitor"); | ||
@@ -31,1 +33,9 @@ exports.Result = visitor_1.Result; | ||
exports.default = createConverter; | ||
function validate(value, opt) { | ||
const visitors = [validation_1.requiredValidationVisitor, validation_1.validatorVisitor]; | ||
for (const visitor of (opt.visitors || [])) { | ||
visitors.push(visitor); | ||
} | ||
return convert(value, { type: opt.type, decorators: opt.decorators, path: opt.path, visitors }); | ||
} | ||
exports.validate = validate; |
@@ -11,3 +11,3 @@ import { VisitorInvocation } from './invocation'; | ||
} | ||
declare function validate(validator: Validator | string | symbol): (target: any, name: string, index?: any) => void; | ||
declare function createValidation(validator: Validator | string | symbol): (target: any, name: string, index?: any) => void; | ||
declare function optional(): (target: any, name: string, index?: any) => void; | ||
@@ -20,2 +20,3 @@ declare function partial(type: Class): (...args: any[]) => void; | ||
declare function requiredValidationVisitor(i: VisitorInvocation): Result; | ||
export { validate, optional, partial, requiredValidationVisitor, Validator, ValidatorDecorator, OptionalValidator, PartialValidator }; | ||
declare function validatorVisitor(i: VisitorInvocation): Result; | ||
export { createValidation, optional, partial, requiredValidationVisitor, validatorVisitor, Validator, ValidatorDecorator, OptionalValidator, PartialValidator }; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const visitor_1 = require("./visitor"); | ||
const converter_1 = require("./converter"); | ||
const OptionalValidator = Symbol("tc:optional"); | ||
@@ -11,12 +12,12 @@ exports.OptionalValidator = OptionalValidator; | ||
exports.PartialValidator = PartialValidator; | ||
function validate(validator) { | ||
function createValidation(validator) { | ||
return tinspector_1.decorateProperty({ type: "tc:validator", validator }); | ||
} | ||
exports.validate = validate; | ||
exports.createValidation = createValidation; | ||
function optional() { | ||
return validate(OptionalValidator); | ||
return createValidation(OptionalValidator); | ||
} | ||
exports.optional = optional; | ||
function partial(type) { | ||
return tinspector_1.mergeDecorator(tinspector_1.default.type(type, "Partial"), validate(PartialValidator)); | ||
return tinspector_1.mergeDecorator(tinspector_1.default.type(type, "Partial"), createValidation(PartialValidator)); | ||
} | ||
@@ -39,1 +40,24 @@ exports.partial = partial; | ||
exports.requiredValidationVisitor = requiredValidationVisitor; | ||
function getValidators(i) { | ||
return i.decorators | ||
.filter((x) => x.validator !== OptionalValidator | ||
&& x.validator !== PartialValidator && x.type === "tc:validator"); | ||
} | ||
function validatorVisitor(i) { | ||
const validators = getValidators(i); | ||
if (validators.length > 0) { | ||
const result = i.proceed(); | ||
const messages = []; | ||
for (const validator of validators) { | ||
if (typeof validator.validator === "function") { | ||
const msg = validator.validator(converter_1.safeToString(i.value)); | ||
if (msg) | ||
messages.push(msg); | ||
} | ||
} | ||
return messages.length > 0 ? visitor_1.Result.error(i.path, messages) : result; | ||
} | ||
else | ||
return i.proceed(); | ||
} | ||
exports.validatorVisitor = validatorVisitor; |
{ | ||
"name": "typedconverter", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "Convert object into classes match with TypeScript type annotation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
37767
21
747
0