当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript ajv.validate函数代码示例

本文整理汇总了TypeScript中ajv.validate函数的典型用法代码示例。如果您正苦于以下问题:TypeScript validate函数的具体用法?TypeScript validate怎么用?TypeScript validate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了validate函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: get

export const onPreHandler = (request: Request, h: ResponseToolkit) => {
	const config = get(request.route.settings.plugins, "jsonapi.validate");

	if (!config) {
		return h.continue;
	}

	const ajv = new AJV();

	for (const type of ["headers", "params", "query", "payload"]) {
		const schema = config[type];

		if (schema) {
			// @TODO: enable this later on
			// if (type !== "headers") {
			// 	schema.additionalProperties = false;
			// }

			if (!ajv.validate(schema, request[type])) {
				return h
					.response({
						errors: ajv.errors ? mapErrors(type, ajv.errors) : [],
					})
					.code(422)
					.takeover();
			}
		}
	}

	return h.continue;
};
开发者ID:hapist,项目名称:json-api,代码行数:31,代码来源:validator.ts

示例2: Ajv

 const test = (obj: any) => {
   const ajv = new Ajv(this.options);
   const valid = ajv.validate(schema, obj);
   if (!valid) {
     throw this.buildErrors(ajv.errors!, targetType);
   }
 };
开发者ID:Romakita,项目名称:ts-express-decorators,代码行数:7,代码来源:AjvService.ts

示例3: Ajv

 return fs.readJSON(path.join(respath, schemaName + ".json")).then(function (schema) {
     const ajv = new Ajv({ schemaId: 'auto', allErrors: true });
     const metaschema = require('ajv/lib/refs/json-schema-draft-04.json');
     ajv.addMetaSchema(metaschema);
     if (!ajv.validate(schema, data)) {
         throw ("JSON schema errors: " + JSON.stringify(ajv.errors, null, 2));
     }
     return data;
 })
开发者ID:crepi22,项目名称:dccjs,代码行数:9,代码来源:constants.ts

示例4: validate

export function validate(spec: Spec) {
  const ajv = new AJV({ validateSchema: true });

  /* tslint:disable */
  ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
  const openAPIJSONSchema = require('swagger-schema-official/schema.json');
  /* tslint:enable */

  const result = ajv.validate(openAPIJSONSchema, spec);

  return {
    valid: result,
    errors: (ajv.errors || []).slice()
  };
}
开发者ID:tyranid-org,项目名称:tyranid,代码行数:15,代码来源:utils.ts

示例5: handleSetConfig

 handleSetConfig(newConfig: any): Promise<{ status: any, statusCode: number }> {
   const ajv = new Ajv()
   const valid = ajv.validate(GAIA_CONFIG_SCHEMA, newConfig)
   if (!valid) {
     logger.error(`Failed to validate Gaia configuration: ${JSON.stringify(ajv.errors)}`)
     const ret = { 
       statusCode: 400, 
       status: { 
         error: 'Invalid Gaia configuration',
         more: JSON.parse(JSON.stringify(ajv.errors))
       }
     }
     return Promise.resolve().then(() => ret)
   }
   return this.handleSetFields(newConfig, Object.keys(GAIA_CONFIG_SCHEMA.properties))
 }
开发者ID:blockstack,项目名称:blockstack-registrar,代码行数:16,代码来源:server.ts

示例6: matchValue

export function matchValue(val: any, schema: object) {
    if (schema === null) return val === schema;
    if (typeof schema !== "object") {
        console.warn(`Expected schema to be an object, but got ${typeof schema} instead`);
        return false;
    }

    const validator = new Ajv();
    validator.validateSchema(schema);
    const result = validator.validate(schema, val);
    if (result === true) {
        return true;
    } else if (result === false) {
        return false;
    } else {
        throw new Error("Validation required a schema with $ref...only synchronous validation is supported");
    }
}
开发者ID:xogeny,项目名称:denada-js,代码行数:18,代码来源:denada.ts

示例7: validate

export function validate(data: object) {
  const ajv = new Ajv();
  return ajv.validate(sceneSchema, data);
}
开发者ID:7h1b0,项目名称:Anna,代码行数:4,代码来源:scene.ts

示例8: Ajv

/// <reference types="ajv" />

import * as Ajv from 'ajv';
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
var validate = ajv.compile({});
var valid = validate({});
if (!valid) console.log(validate.errors);

var valid = ajv.validate({}, {});
if (!valid) console.log(ajv.errors);

ajv.addSchema({}, 'mySchema');
var valid = ajv.validate('mySchema', {});
if (!valid) console.log(ajv.errorsText());

ajv.addKeyword('range', {
    type: 'number', compile: function (sch, parentSchema) {
        var min: any = sch[0];
        var max: any = sch[1];

        return parentSchema.exclusiveRange === true
            ? function (data) { return data > min && data < max; }
            : function (data) { return data >= min && data <= max; }
    }
});

var schema = { "range": [2, 4], "exclusiveRange": true };
var validate = ajv.compile(schema);
console.log(validate(2.01)); // true
console.log(validate(3.99)); // true
console.log(validate(2)); // false
开发者ID:longlho,项目名称:DefinitelyTyped,代码行数:31,代码来源:ajv-tests.ts

示例9: validate

export function validate(data: unknown) {
  const ajv = new Ajv();
  return ajv.validate(roomSchema, data);
}
开发者ID:7h1b0,项目名称:Anna,代码行数:4,代码来源:room.ts


注:本文中的ajv.validate函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。