@nrfcloud/ts-json-schema-transformer
Version:
A TypeScript transformer that generates JSON schemas and validators from TypeScript interfaces
235 lines • 6.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationError = void 0;
exports.getSchema = getSchema;
exports.validate = validate;
exports.createValidateFn = createValidateFn;
exports.guard = guard;
exports.createGuardFn = createGuardFn;
exports.createValidator = createValidator;
exports.getMockObject = getMockObject;
exports.mock = mock;
exports.createMockFn = createMockFn;
exports.assertValid = assertValid;
exports.assertGuard = assertGuard;
exports.createAssertGuardFn = createAssertGuardFn;
exports.assert = assert;
exports.createAssertFn = createAssertFn;
exports.parse = parse;
exports.createParseFn = createParseFn;
exports.assertParse = assertParse;
exports.createAssertParseFn = createAssertParseFn;
exports.__validation = __validation;
exports.__parser = __parser;
exports.noop = noop;
exports.validationAssertion = validationAssertion;
/**
* Get the JSON schema for the provided type
* @transformer ts-json-schema-transformer
*/
function getSchema() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Validate an object according to the provided type
* Returns the object back if it is valid or undefined if it is not
* @transformer ts-json-schema-transformer
*/
function validate(_obj) {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Get a reusable validator for the provided type
* Returns the object back if it is valid or undefined if it is not
* @transformer ts-json-schema-transformer
*/
function createValidateFn() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Validate an object according to the provided type
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
function guard(_obj) {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Get a reusable validator for the provided type
* This function is an AJV validator, so errors are stored in the `errors` property
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
function createGuardFn() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Get a validator for the provided type
* @deprecated use `createValidateFn`
* @transformer ts-json-schema-transformer
*/
function createValidator() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Get a mock object for the provided type
* @deprecated use `mock`
* @transformer ts-json-schema-transformer
*/
function getMockObject() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Get a mock object for the provided type
* @transformer ts-json-schema-transformer
*/
function mock() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* @transformer ts-json-schema-transformer
*/
function createMockFn() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Assert that an object is valid according to the provided type
* Acts as a type guard
* @deprecated use `assertGuard`
* @transformer ts-json-schema-transformer
*/
function assertValid(_obj) {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Assert that an object is valid according to the provided type
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
function assertGuard(_obj) {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Create an assert guard function for the provided type
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
function createAssertGuardFn() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Assert that an object is valid and returns the object back
* @transformer ts-json-schema-transformer
*/
function assert(_obj) {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Create an assert function for the provided type
* @transformer ts-json-schema-transformer
*/
function createAssertFn() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Parse a string into the provided type
* Returns undefined if the string is not valid
* @transformer ts-json-schema-transformer
*/
function parse(_input) {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Create a parse function for the provided type
* Returns undefined if the string is not valid
* @transformer ts-json-schema-transformer
*/
function createParseFn() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Assert that a string can be parsed into the provided type, then returns it
* @transformer ts-json-schema-transformer
*/
function assertParse(_input) {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
/**
* Create an assert parse function for the provided type
* @transformer ts-json-schema-transformer
*/
function createAssertParseFn() {
throw new Error("Not implemented. Did you forget to run the transformer?");
}
class ValidationError extends Error {
}
exports.ValidationError = ValidationError;
/**
* @internal
*/
function __validation(validator, shouldThrow, shouldReturn, obj) {
const result = validator(obj);
switch (true) {
case result && shouldReturn === "object":
return obj;
case result && shouldReturn === "boolean":
return true;
case result && shouldReturn === "none":
return undefined;
case !result && shouldThrow:
throw new ValidationError(`Validation error: ${validator.errors?.map(error => JSON.stringify(error)).join(", ")}`, {
cause: validator.errors,
});
case !result && shouldReturn === "object":
return undefined;
case !result && shouldReturn === "boolean":
return false;
}
}
/**
* @internal
*/
function __parser(validator, shouldThrow, input) {
const parsed = JSON.parse(input);
return __validation(validator, shouldThrow, "object", parsed);
}
/**
* @internal
*/
function noop(arg) {
return arg;
}
/**
* @internal
* @deprecated
*/
function validationAssertion(validator, obj) {
if (!validator(obj)) {
throw new ValidationError(`Validation error: ${validator.errors?.map(error => JSON.stringify(error)).join(", ")}`, {
cause: validator.errors,
});
}
return obj;
}
exports.default = {
getSchema,
validate,
createValidateFn,
guard,
createGuardFn,
createValidator,
getMockObject,
mock,
createMockFn,
assertValid,
assertGuard,
createAssertGuardFn,
assert,
createAssertFn,
parse,
createParseFn,
assertParse,
createAssertParseFn,
ValidationError,
};
//# sourceMappingURL=index.js.map