本文整理汇总了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;
};
示例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);
}
};
示例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;
})
示例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()
};
}
示例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))
}
示例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");
}
}
示例7: validate
export function validate(data: object) {
const ajv = new Ajv();
return ajv.validate(sceneSchema, data);
}
示例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
示例9: validate
export function validate(data: unknown) {
const ajv = new Ajv();
return ajv.validate(roomSchema, data);
}