Merge branch 'yume-chan-master'

master
Evgeny Poberezkin 2019-01-03 20:35:13 +00:00
commit 1efb91fe5d
3 changed files with 94 additions and 28 deletions

60
lib/ajv.d.ts vendored
View File

@ -1,12 +1,36 @@
declare var ajv: {
(options?: ajv.Options): ajv.Ajv;
new (options?: ajv.Options): ajv.Ajv;
ValidationError: ValidationError;
MissingRefError: MissingRefError;
new(options?: ajv.Options): ajv.Ajv;
ValidationError: typeof AjvErrors.ValidationError;
MissingRefError: typeof AjvErrors.MissingRefError;
$dataMetaSchema: object;
}
declare namespace AjvErrors {
class ValidationError extends Error {
constructor(errors: Array<ajv.ErrorObject>);
message: string;
errors: Array<ajv.ErrorObject>;
ajv: true;
validation: true;
}
class MissingRefError extends Error {
constructor(baseId: string, ref: string, message?: string);
static message: (baseId: string, ref: string) => string;
message: string;
missingRef: string;
missingSchema: string;
}
}
declare namespace ajv {
type ValidationError = AjvErrors.ValidationError;
type MissingRefError = AjvErrors.MissingRefError;
interface Ajv {
/**
* Validate data using schema
@ -268,11 +292,11 @@ declare namespace ajv {
}
type ErrorParameters = RefParams | LimitParams | AdditionalPropertiesParams |
DependenciesParams | FormatParams | ComparisonParams |
MultipleOfParams | PatternParams | RequiredParams |
TypeParams | UniqueItemsParams | CustomParams |
PatternRequiredParams | PropertyNamesParams |
IfParams | SwitchParams | NoParams | EnumParams;
DependenciesParams | FormatParams | ComparisonParams |
MultipleOfParams | PatternParams | RequiredParams |
TypeParams | UniqueItemsParams | CustomParams |
PatternRequiredParams | PropertyNamesParams |
IfParams | SwitchParams | NoParams | EnumParams;
interface RefParams {
ref: string;
@ -344,29 +368,11 @@ declare namespace ajv {
caseIndex: number;
}
interface NoParams {}
interface NoParams { }
interface EnumParams {
allowedValues: Array<any>;
}
}
declare class ValidationError extends Error {
constructor(errors: Array<ajv.ErrorObject>);
message: string;
errors: Array<ajv.ErrorObject>;
ajv: true;
validation: true;
}
declare class MissingRefError extends Error {
constructor(baseId: string, ref: string, message?: string);
static message: (baseId: string, ref: string) => string;
message: string;
missingRef: string;
missingSchema: string;
}
export = ajv;

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