當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。