Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parameters validation #451

Merged
merged 4 commits into from
Jul 14, 2020
Merged

feat: parameters validation #451

merged 4 commits into from
Jul 14, 2020

Conversation

stone-jin
Copy link
Member

@stone-jin stone-jin commented Mar 30, 2020

  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

使用方法:

import { Check, Rule, RuleType } from '../../src';
import * as assert from 'assert';
describe('/test/annotation/check.test.ts', () => {
  it('check with check', () => {
    // 规则通过Rule进行定义
    class UserDTO {
      @Rule(RuleType.number().max(10))
      age: number;
    }

    class Hello {
      // 调用函数的时候,会根据参数的类型,检测是否定义了Rule进行校验
      @Check()
      school(a, data: UserDTO) {
        return data;
      }
    }
    const user = {
      age: 8
    };
    const result = new Hello().school(1, user);
    assert.deepEqual(result, user);
  });

  it('check with no check', () => {
    class UserDTO {
      @Rule(RuleType.number().max(10))
      age: number;
    }

    class Hello {
      school(a, data: UserDTO) {
        return data;
      }
    }
    const user = {
      age: 18
    };
    const result = new Hello().school(1, user);
    assert.deepEqual(result, user);
  });

  it('check with check when vo have two level', () => {
    class WorldDTO {
      @Rule(RuleType.number().max(20))
      age: number;
    }

    class UserDTO {
      @Rule(RuleType.number().max(10))
      age: number;

      // VO有嵌套的情况
      @Rule(WorldDTO)
      world: WorldDTO;
    }

    class Hello {
      @Check()
      school(a, data: UserDTO) {
        return data;
      }
    }
    const user = {
      age: 10,
      world: {
        age: 18
      }
    };
    const result = new Hello().school(1, user);
    assert.deepEqual(result, user);
  });

  it('check with check when vo have two level not equal', () => {
    class WorldDTO {
      @Rule(RuleType.number().max(20))
      age: number;
    }

    class UserDTO {
      @Rule(RuleType.number().max(10))
      age: number;


      @Rule(WorldDTO)
      world: WorldDTO;
    }

    class Hello {
      @Check()
      school(a, data: UserDTO) {
        return data;
      }
    }
    const user = {
      age: 10,
      world: {
        age: 22
      }
    };
    const result = new Hello().school(1, user);
    assert.notDeepEqual(result, user);
  });

  it('check with check when two level and array and not equal', () => {
    class WorldDTO {
      @Rule(RuleType.number().max(20))
      age: number;
    }

    class UserDTO {
      @Rule(RuleType.number().max(10))
      age: number;

       // VO有嵌套、并且是数组的情况
      @Rule(WorldDTO)
      worlds: WorldDTO[];
    }

    class Hello {
      @Check()
      school(a, data: UserDTO) {
        return data;
      }
    }
    const user = {
      age: 10,
      worlds: [{
        age: 22
      }]
    };
    const result = new Hello().school(1, user);
    assert.notDeepEqual(result, user);
  });
});

@czy88840616 czy88840616 changed the title 参数校验 feat: parameters validation Jul 14, 2020
@czy88840616 czy88840616 merged commit 92735b0 into 2.x Jul 14, 2020
@czy88840616 czy88840616 deleted the feat/validator branch July 14, 2020 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants