Skip to content

Commit

Permalink
Merge pull request #21 from vodyani/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
ChoGathK authored Sep 7, 2022
2 parents 65b511d + 9eab212 commit 3cc8cf1
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 97 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [8.2.2-beta.1](https://github.com/vodyani/class-decorator/compare/v8.2.1...v8.2.2-beta.1) (2022-09-07)


### Bug Fixes

* delete unnecessary types ([985aab8](https://github.com/vodyani/class-decorator/commit/985aab84023a4184f61095d33af2570697e068d6))

## [8.2.1](https://github.com/vodyani/class-decorator/compare/v8.2.0...v8.2.1) (2022-08-31)


Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vodyani/class-decorator",
"license": "MIT",
"version": "8.2.1",
"version": "8.2.2-beta.1",
"author": "ChoGathK",
"description": "🏭 class-decorator is an easy-to-use toolbox that provides class validation, conversion, and property binding functions",
"homepage": "https://github.com/vodyani/class-decorator#readme",
Expand Down Expand Up @@ -61,7 +61,7 @@
]
},
"dependencies": {
"@vodyani/utils": "^8.5.1",
"@vodyani/utils": "^8.5.2",
"autobind-decorator": "2.4.0",
"class-transformer": "0.5.1",
"class-validator": "0.13.2",
Expand Down
8 changes: 4 additions & 4 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const RequiredKey = Symbol('Vodyani.Validator.MetadataKey.RequiredKey');
export const ValidatedKey = Symbol('Vodyani.Validator.MetadataKey.ValidatedKey');
export const EachValidatedKey = Symbol('Vodyani.Validator.MetadataKey.EachValidated');
export const CustomValidatedKey = Symbol('Vodyani.Validator.MetadataKey.CustomValidatedKey');
export const RequiredKey = Symbol('RequiredKey');
export const ValidatedKey = Symbol('ValidatedKey');
export const EachValidatedKey = Symbol('EachValidated');
export const CustomValidatedKey = Symbol('CustomValidatedKey');
12 changes: 11 additions & 1 deletion src/common/declare.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
export * from 'class-transformer';
import { boundMethod } from 'autobind-decorator';

/**
* This decorator is used to make the methods of a class forcibly bind to the `this` property.
*
* @see [autobind-decorator](https://www.npmjs.com/package/autobind-decorator)
*
* @publicApi
*/
export const This = boundMethod;
export * from 'class-validator';
export * from 'class-transformer';
29 changes: 1 addition & 28 deletions src/common/interface.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import { Method } from '@vodyani/utils';
import { ValidatorOptions } from 'class-validator';

import { ClassTransformOptions } from './declare';

/**
* The class for typescript.
*/
export interface Class<T = any> extends Function {
new (...args: any[]): T;
}
/**
* Choices for conversion functions.
*/
export interface ConvertOptions {
/**
* When the received value does not correspond to expectations, this value is returned.
*
* @default null
*/
default?: any;
/**
* The conversion is carried out if the outcome of the conditional validation function execution is true.
*
* @empale (num: number) => num > 0
*/
condition?: Method;
/**
* The process that carries out the transition.
*
* @empale (data: any) => Number(data)
*/
transformer?: Method;
}

export interface ValidateMetaData {
/** The argument index. */
Expand All @@ -41,7 +14,7 @@ export interface ValidateMetaData {
/** The error message. */
message?: string;
/** The custom validation function */
validator?: Method<boolean>;
validator?: (...args: any[]) => boolean;
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/decorator/class.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/decorator/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './class';
export * from './transform';
export * from './validate';
13 changes: 6 additions & 7 deletions src/decorator/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isPromise } from 'util/types';

import { Method } from '@vodyani/utils';
import { ClassTransformOptions, Transform } from 'class-transformer';

import { Class } from '../common';
Expand All @@ -19,7 +18,7 @@ import { toAssemble } from '../method';
* @publicApi
*/
export function Assemble(type: Class, options?: ClassTransformOptions) {
return function(_target: any, _property: string, descriptor: TypedPropertyDescriptor<Method>) {
return function(_target: any, _property: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>) {
const method = descriptor.value;

descriptor.value = function(...args: any[]) {
Expand All @@ -43,7 +42,7 @@ export function Assemble(type: Class, options?: ClassTransformOptions) {
*
* @publicApi
*/
export function TransformValue(transformer: Method, ...args: []) {
export function TransformValue(transformer: Function, ...args: []) {
return Transform(({ value }) => transformer(value, ...args));
}
/**
Expand Down Expand Up @@ -101,8 +100,8 @@ export function TransformMap(type: Class, options?: ClassTransformOptions) {
*
* @publicApi
*/
export function ResultTransformer(transformer: Method) {
return function (_target: any, _property: string, descriptor: TypedPropertyDescriptor<Method>) {
export function ResultTransformer(transformer: Function) {
return function (_target: any, _property: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>) {
const method = descriptor.value;

descriptor.value = function(...args: any[]) {
Expand All @@ -120,8 +119,8 @@ export function ResultTransformer(transformer: Method) {
*
* @publicApi
*/
export function ArgumentTransformer(transformer: Method) {
return function (_target: any, _property: string, descriptor: TypedPropertyDescriptor<Method>) {
export function ArgumentTransformer(transformer: Function) {
return function (_target: any, _property: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>) {
const method = descriptor.value;

descriptor.value = function(...args: any[]) {
Expand Down
8 changes: 3 additions & 5 deletions src/decorator/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Method } from '@vodyani/utils';

import {
toValidated,
toEachValidate,
Expand Down Expand Up @@ -87,7 +85,7 @@ export function EachValidated(type: Class) {
*
* @publicApi
*/
export function CustomValidated(validator: Method<boolean>, message: string) {
export function CustomValidated(validator: (...args: any[]) => boolean, message: string) {
return function(target: any, property: any, index: number) {
const data = getReflectOwnMetadata(CustomValidatedKey, target, property);
const metadata: ValidateMetaData = { index, message, validator };
Expand All @@ -105,7 +103,7 @@ export function CustomValidated(validator: Method<boolean>, message: string) {
* @publicApi
*/
export function ArgumentValidator(error: Class<Error> = Error) {
return function(target: any, property: string, descriptor: TypedPropertyDescriptor<Method<any>>) {
return function(target: any, property: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>) {
const method = descriptor.value;

descriptor.value = function(...args: any[]) {
Expand All @@ -127,7 +125,7 @@ export function ArgumentValidator(error: Class<Error> = Error) {
* @publicApi
*/
export function AssembleValidator(options?: ArgumentValidateOptions) {
return function(target: any, property: string, descriptor: TypedPropertyDescriptor<Method<Promise<any>>>) {
return function(target: any, property: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>) {
const method = descriptor.value;
const error = options?.error || Error;

Expand Down
7 changes: 2 additions & 5 deletions test/convert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ describe('convert', () => {
it('ResultTransformer', async () => {
class Demo {
@ResultTransformer(toString)
// @ts-ignore
public getName() {
return null;
return null as any;
}

@ResultTransformer(toNumber)
// @ts-ignore
public async asyncGetName() {
return null;
return null as any;
}
}

Expand All @@ -33,7 +31,6 @@ describe('convert', () => {

class Demo {
@ArgumentTransformer(fn)
// @ts-ignore
public returnAge(age: number) {
return age;
}
Expand Down
10 changes: 0 additions & 10 deletions test/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { ValidateNested, TransformSet, TransformMap, IsNotEmpty, toValidateClass

describe('test', () => {
it('toValidateClass', async () => {
// @ts-ignore
class DICT { @IsNotEmpty({ message: 'test' }) public name: string; }

// @ts-ignore
class BASE { @ValidateNested() @Type(() => DICT) public dict: DICT[]; }

const Base = new BASE();
Expand All @@ -21,27 +18,20 @@ describe('test', () => {

it('toValidateClass', async () => {
class DEMO {
// @ts-ignore
@Expose() @IsNotEmpty() @IsNumber({ allowNaN: false }) test: number;
}

class Test {
// @ts-ignore
@IsNotEmpty() @IsNumber({ allowNaN: false }) @Expose() age: number;
// @ts-ignore
@IsNotEmpty() @IsString() @Expose() name: string;
}

class PartOfDemo extends PickType(Test, ['name']) {}

class Dict {
// @ts-ignore
@ValidateNested() @Expose() @Type(() => PartOfDemo) public item: PartOfDemo;
// @ts-ignore
@ValidateNested({ each: true }) @Expose() @Type(() => PartOfDemo) public array: PartOfDemo[];
// @ts-ignore
@ValidateNested({ each: true }) @Expose() @TransformSet(PartOfDemo) public set: Set<PartOfDemo>;
// @ts-ignore
@ValidateNested({ each: true }) @Expose() @TransformMap(PartOfDemo) public map: Map<string, PartOfDemo>;
}

Expand Down
15 changes: 0 additions & 15 deletions test/transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,27 @@ import { Expose, Exclude, Type } from 'class-transformer';
import { toAssemble, Assemble, TransformValue, TransformMap, TransformSet } from '../src';

class User {
// @ts-ignore
@Expose() public name: string;
// @ts-ignore
@Expose() @TransformValue(toNumber) public age: number;
}

class PartOfUser extends PickType(User, ['age']) {}

class ExcludeDemo {
// @ts-ignore
title: string;
// @ts-ignore
@Exclude() password: string;
// @ts-ignore
@Expose() @TransformValue(toString) name: string;
}

class Demo {
// @ts-ignore
@Expose() @Type(() => User) public user: User;
// @ts-ignore
@Expose() @Type(() => User) public userArray: User[];
// @ts-ignore
@Expose() @TransformSet(User) public userSet: Set<User>;
// @ts-ignore
@Expose() @TransformMap(User) public userMap: Map<string, User>;
}

class Service {
@Assemble(Demo)
// @ts-ignore
public async getDemo(): Promise<Demo> {
const user = { name: 'vodyani', age: 'USER' };
const userArray = [{ age: '20' }];
Expand All @@ -52,7 +42,6 @@ class Service {
}

@Assemble(ExcludeDemo)
// @ts-ignore
public getExcludeDemo(): ExcludeDemo {
return Object({ password: '123' });
}
Expand Down Expand Up @@ -93,13 +82,9 @@ describe('test class', () => {
expect(result2).toEqual({ age: 0 });

class User2 {
// @ts-ignore
@Expose() @TransformValue((name: string) => toString(name, 'demo')) public name: string;
// @ts-ignore
@Expose() @TransformValue(toNumber) public age: number;
// @ts-ignore
@Expose() public setData: Set<string>;
// @ts-ignore
@Expose() public arrayData: Array<string>;
}

Expand Down
5 changes: 5 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.spec.ts"],
"exclude": ["node_modules", "dist"],
}

0 comments on commit 3cc8cf1

Please sign in to comment.