diff --git a/package.json b/package.json index 0646971..6fbbd53 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/spec/typescript/index.ts b/spec/typescript/index.ts new file mode 100644 index 0000000..0858180 --- /dev/null +++ b/spec/typescript/index.ts @@ -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