Value we want to check to see if it matches Circle.
true if data is confirmed to be a valid instance of type Circle, false otherwise.
If result is false, you may check property validateCircle#errors for an array of ErrorObjects to figure out whatever went wrong.
This example shows you how you can verify that a value matches type Circle.
//A function that can only operate on Circle.
//Will print it to the console.
function printObject(obj: Circle): void {
console.log(obj);
}
//Some object we want to validate.
const obj: unknown = {
someProperty: 25
};
//If this object is a valid instance of Circle.
if (validateCircle(obj)) {
//We can use it as the type.
printObject(obj);
}
OptionalerrorsIf the validation function returns false, this property will be set to an array of ErrorObjects, explaining why validation failed.
If your project contains package ajv, then this function will use AJV's interface for them. If not, it will use an internal and independant interface, StandaloneErrorObject, to define them. Note that there is no runtime difference between the two types.
This example shows you how to fetch the resulting ErrorObject array if something goes wrong during validation.
//Some object we want to validate.
const obj: unknown = {
someProperty: 25
};
//If this object is NOT a valid instance of Circle.
if (!validateCircle(obj)) {
//Tell user validation failed.
console.log("Obj is NOT an instance of Circle");
//Fetch the list of errors.
const errors = validateCircle.errors;
//By default, the error messages are in English.
//Use ajv-i18n to localize the error messages.
//This localize function was imported from "ajv-i18n/localize/fr"
//and will translate the error messages to French.
localize(errors);
//Iterate the errors.
for (const error of errors) {
//Print the error representing whatever went wrong.
console.log(error);
}
}
Checks whether a given value is a valid instance of Circle and matches its JSON Schema.