本文整理匯總了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);
}