konditions
Version:
A simple and customizable JSON-based condition engine in TypeScript (e.g. GreaterThan, StringLike, Every, Some)
68 lines • 2.46 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseWithZodAndBuildResponse = exports.parseInputPropsWithZodAndBuildResponse = void 0;
const z = __importStar(require("zod"));
const parseInputPropsWithZodAndBuildResponse = (props) => {
const schema = z
.object({
type: z.string(),
})
.nonstrict();
return exports.parseWithZodAndBuildResponse(schema, props);
};
exports.parseInputPropsWithZodAndBuildResponse = parseInputPropsWithZodAndBuildResponse;
const parseWithZodAndBuildResponse = (schema, props) => {
try {
return {
passed: true,
props: schema.parse(props),
};
}
catch (error) {
if (error instanceof z.ZodError) {
return {
passed: false,
error: {
type: `ValidationError`,
reasons: error.errors.map((e) => ({
namespace: e.path.join(`/`),
message: e.message,
})),
},
};
}
else {
return {
passed: false,
error: {
type: `UnexpectedError`,
message: `Unexpected problem occured while parsing with Zod (parseWithZodToResolution).`,
metadata: {
originalError: error,
},
},
};
}
}
};
exports.parseWithZodAndBuildResponse = parseWithZodAndBuildResponse;
//# sourceMappingURL=zod.js.map
;