test: add tests for typescript definition

master
Simon Chan 2018-12-13 17:55:00 +08:00
parent ad434b236d
commit d048cfc979
2 changed files with 61 additions and 1 deletions

View File

@ -18,7 +18,7 @@
"test-fast": "AJV_FAST_TEST=true npm run test-spec",
"test-debug": "mocha spec/*.spec.js --debug-brk -R spec",
"test-cov": "nyc npm run test-spec",
"test-ts": "tsc --target ES5 --noImplicitAny lib/ajv.d.ts",
"test-ts": "tsc --target ES5 --noImplicitAny --noEmit spec/typescript/index.ts",
"bundle": "del-cli dist && node ./scripts/bundle.js . Ajv pure_getters",
"bundle-beautify": "node ./scripts/bundle.js js-beautify",
"build": "del-cli lib/dotjs/*.js '!lib/dotjs/index.js' && node scripts/compile-dots.js",

60
spec/typescript/index.ts Normal file
View File

@ -0,0 +1,60 @@
import ajv = require("../..");
// #region new()
const options: ajv.Options = {
verbose: true,
};
let instance: ajv.Ajv;
instance = ajv();
instance = ajv(options);
instance = new ajv();
instance = new ajv(options);
// #endregion new()
// #region validate()
let data = {
foo: 42,
}
let result = instance.validate("", data);
if (typeof result === "boolean") {
// sync
console.log(result);
} else {
// async
result.then(value => {
data = value;
});
}
// #endregion validate()
// #region compile()
const validator = instance.compile({});
result = validator(data);
if (typeof result === "boolean") {
// sync
console.log(result);
} else {
// async
result.then(value => {
data = value;
});
}
// #endregion compile()
// #region errors
const validationError: ajv.ValidationError = new ajv.ValidationError([]);
validationError instanceof ajv.ValidationError;
validationError.ajv === true;
validationError.validation === true;
ajv.MissingRefError.message("", "");
const missingRefError: ajv.MissingRefError = new ajv.MissingRefError("", "", "");
missingRefError instanceof ajv.MissingRefError;
missingRefError.missingRef;
// #endregion