當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript common.JsonSchemesService類代碼示例

本文整理匯總了TypeScript中@tsed/common.JsonSchemesService的典型用法代碼示例。如果您正苦於以下問題:TypeScript JsonSchemesService類的具體用法?TypeScript JsonSchemesService怎麽用?TypeScript JsonSchemesService使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了JsonSchemesService類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: describe

describe("AJV", () => {
  let jsonSchemesService: JsonSchemesService;
  before(
    inject([AjvService, JsonSchemesService], (_ajvService_: AjvService, _jsonSchemesService_: JsonSchemesService) => {
      ajvService = _ajvService_;
      jsonSchemesService = _jsonSchemesService_;
    })
  );

  after(TestContext.reset);

  describe("Date validation", () => {
    const errorMsg = "At TestDate.dateStart should match format \"date-time\"";

    class TestDate {
      @Format("date-time")
      dateStart: Date;
    }

    it("should have expected json schema", () => {
      expect(jsonSchemesService.getSchemaDefinition(TestDate)).to.deep.eq({
        definitions: {},
        properties: {
          dateStart: {
            format: "date-time",
            type: "string"
          }
        },
        type: "object"
      });
    });
    it("should validate data (1)", () => {
      runValidation({}, TestDate).to.be.true;
    });

    it("should not validate data (2)", () => {
      runValidation({dateStart: "1987-07-12 01:00:00"}, TestDate).to.be.eq(errorMsg);
    });
    it("should validate data (3)", () => {
      runValidation({dateStart: new Date().toISOString()}, TestDate).to.be.true;
    });
    it("should not validate data (4)", () => {
      runValidation({dateStart: new Date()}, TestDate).to.be.eq("At TestDate.dateStart should be string");
    });
    it("should not validate data (5)", () => {
      runValidation({dateStart: "test"}, TestDate).to.be.eq(errorMsg);
    });
    it("should validate data (6)", () => {
      runValidation({other: "test"}, TestDate).to.be.true;
    });
  });

  describe("Array of", () => {
    const errorMsg = "At TestDate.dateStart should match format \"date-time\"";

    class TestDate {
      @Format("date-time")
      dateStart: Date;
    }

    it("should have expected json schema", () => {
      expect(jsonSchemesService.getSchemaDefinition(TestDate)).to.deep.eq({
        definitions: {},
        properties: {
          dateStart: {
            format: "date-time",
            type: "string"
          }
        },
        type: "object"
      });
    });
    it("should validate data (1)", () => {
      runValidation([{}], TestDate, Array).to.be.true;
    });
    it("should not validate data (2)", () => {
      runValidation([{dateStart: "1987-07-12 01:00:00"}], TestDate, Array).to.be.eq(errorMsg);
    });
    it("should validate data (3)", () => {
      runValidation([{dateStart: new Date().toISOString()}], TestDate, Array).to.be.true;
    });
  });

  describe("Set of", () => {
    const errorMsg = "At TestDate.dateStart should match format \"date-time\"";

    class TestDate {
      @Format("date-time")
      dateStart: Date;
    }

    it("should have expected json schema", () => {
      expect(jsonSchemesService.getSchemaDefinition(TestDate)).to.deep.eq({
        definitions: {},
        properties: {
          dateStart: {
            format: "date-time",
            type: "string"
          }
        },
//.........這裏部分代碼省略.........
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:101,代碼來源:ajv.spec.ts

示例2: validate

  /**
   *
   * @param obj
   * @param targetType
   * @param baseType
   * @returns {boolean}
   */
  public validate(obj: any, targetType: any, baseType?: any): boolean {
    const schema = this.jsonSchemaService.getSchemaDefinition(targetType) as any;

    if (schema && !(obj === null || obj === undefined)) {
      const collection = baseType ? obj : [obj];
      const options = {
        ignoreCallback: (obj: any, type: any) => type === Date,
        checkRequiredValue: false
      };

      const test = (obj: any) => {
        const ajv = new Ajv(this.options);
        const valid = ajv.validate(schema, obj);
        if (!valid) {
          throw this.buildErrors(ajv.errors!, targetType);
        }
      };

      Object.keys(collection).forEach((key: any) =>
        test(this.converterService.deserialize(collection[key], targetType, undefined, options))
      );
    }

    return true;
  }
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:32,代碼來源:AjvService.ts

示例3: it

 it("should have expected json schema", () => {
   expect(jsonSchemesService.getSchemaDefinition(TestDate)).to.deep.eq({
     definitions: {},
     properties: {
       dateStart: {
         format: "date-time",
         type: "string"
       }
     },
     type: "object"
   });
 });
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:12,代碼來源:ajv.spec.ts


注:本文中的@tsed/common.JsonSchemesService類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。