const ajv = new Ajv({loadSchema: loadSchema})
ajv.compileAsync(schema).then(function (validate) {
const valid = validate(data)
// ...
})
async function loadSchema(uri) {
const res = await request.json(uri)
if (res.statusCode >= 400) throw new Error("Loading error: " + res.statusCode)
return res.body
}
const ajv = new Ajv({processCode: transpileFunc})
const validate = ajv.compile(schema) // transpiled es7 async function
validate(data).then(successFunc).catch(errorFunc)
module.exports = async function (options) {
//...
return Promise.all([getSchema(options), getContent]).then(
([schema, json]) => {
const ajv = new Ajv({ extendRefs: true });
const valid = ajv.validate(schema, json);
if (valid) return json;
ajv.errors.forEach((error) =>
logger.error(error.message, error.dataPath)
);
throw new Error("Invalid dependencies");
}
);
}
async function test() {
//...
const schema = JSON.parse(await fs.readFile("./schema/buttplug-schema.json", "utf-8"));
const validator = new ajv();
validator.addMetaSchema(require("ajv/lib/refs/json-schema-draft-06.json"));
const jsonValidator = validator.compile(schema);
//...
}
const fs = require("fs")
const path = require("path")
const Ajv = require("ajv")
const standaloneCode = require("ajv/dist/standalone").default
const schema = {
$id: "https://example.com/bar.json",
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
bar: {type: "string"},
},
"required": ["bar"]
}
// The generated code will have a default export:
// `module.exports = <validateFunctionCode>;module.exports.default = <validateFunctionCode>;`
const ajv = new Ajv({code: {source: true}})
const validate = ajv.compile(schema)
let moduleCode = standaloneCode(ajv, validate)
// Now you can write the module code to file
fs.writeFileSync(path.join(__dirname, "../consume/validate-cjs.js"), moduleCode)