本文整理汇总了TypeScript中jsonschema.Validator.validate方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Validator.validate方法的具体用法?TypeScript Validator.validate怎么用?TypeScript Validator.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonschema.Validator
的用法示例。
在下文中一共展示了Validator.validate方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: save
function save(file: string, schema: any, data: any) {
const result = validator.validate(data, schema);
if (result.errors.length > 0) {
throw result.errors;
}
const content = JSON.stringify(data, null, 2);
fs.writeFileSync(file, content, "utf8");
}
示例2: getValidationErrors
export function getValidationErrors(commandKey: string, commandConfig: any, commandSchema: any): string[] {
const validator = new Validator();
const result = validator.validate(commandConfig, commandSchema);
const errors = result.errors.map((err: any) => {
let message = err.stack;
message = message.replace(' enum ', ' expected ');
message = message.replace('instance', `${commandKey} config`);
return message;
});
return errors;
}
示例3: validateAnalysis
export function validateAnalysis(analyzedPackage: Analysis|null|undefined) {
const result = validator.validate(analyzedPackage, schema);
if (result.throwError) {
throw result.throwError;
}
if (result.errors.length > 0) {
throw new ValidationError(result);
}
if (!/^1\.\d+\.\d+$/.test(analyzedPackage!.schema_version)) {
throw new Error(
`Invalid schema_version in AnalyzedPackage. ` +
`Expected 1.x.x, got ${analyzedPackage!.schema_version}`);
}
}
示例4: load
function load(file: string, schema: any) {
if (!fs.existsSync(file)) {
return undefined;
}
try {
const data = fs.readFileSync(file, "utf8");
const obj = JSON.parse(data);
const result = validator.validate(obj, schema);
if (result.errors.length === 0) {
return obj;
} else {
console.error(`invalid JSON data found in ${name}`, result.errors);
return undefined;
}
} catch (e) {
console.error(`failed to load JSON data from ${name}`, e);
return undefined;
}
}
示例5: validate
export function validate (element: any, schema: any) {
let v = new Validator();
return v.validate(element, schema);
}
示例6: validate
// In order to validate a complex JS object using jsonschema, we must replace any complex
// sub-types (e.g BigNumber) with a simpler string representation. Since BigNumber and other
// complex types implement the `toString` method, we can stringify the object and
// then parse it. The resultant object can then be checked using jsonschema.
/**
* Validate the JS object conforms to a specific JSON schema
* @param instance JS object in question
* @param schema Schema to check against
* @returns The results of the validation
*/
public validate(instance: any, schema: Schema): ValidatorResult {
const jsonSchemaCompatibleObject = JSON.parse(JSON.stringify(instance));
return this._validator.validate(jsonSchemaCompatibleObject, schema);
}
示例7: validate
public validate(instance: object, schema: Schema) {
return this.validator.validate(instance, schema);
}
示例8: Validator
import { Validator, IJSONSchemaValidationError } from "jsonschema";
const v: Validator = new Validator();
const validationResults: { errors: Array<IJSONSchemaValidationError> } = v.validate("Smith", {"type": "string"});