@nucypher/taco
Version:
### [`nucypher/taco-web`](../../README.md)
69 lines • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.httpsURLSchema = exports.jsonPathSchema = exports.baseConditionSchema = exports.UserAddressSchema = exports.plainStringSchema = void 0;
const jsonpath_1 = require("@astronautlabs/jsonpath");
const taco_auth_1 = require("@nucypher/taco-auth");
const zod_1 = require("zod");
const const_1 = require("../const");
// We want to discriminate between ContextParams and plain strings
// If a string starts with `:`, it's a ContextParam
exports.plainStringSchema = zod_1.z
.string()
.refine((str) => {
return !str.startsWith(const_1.CONTEXT_PARAM_PREFIX);
}, {
message: `String must not be a context parameter i.e. not start with "${const_1.CONTEXT_PARAM_PREFIX}"`,
})
.describe(`Any string that is not a Context Parameter i.e. does not start with \`${const_1.CONTEXT_PARAM_PREFIX}\`.`);
exports.UserAddressSchema = zod_1.z
.literal(taco_auth_1.USER_ADDRESS_PARAM_DEFAULT)
.describe('This is a context variable that will be replaced at decryption time. It represents the Ethereum address of the requester attempting decryption.');
exports.baseConditionSchema = zod_1.z.object({
conditionType: zod_1.z.string(),
});
// Source: https://github.com/colinhacks/zod/issues/831#issuecomment-1063481764
const createUnion = (values) => {
const zodLiterals = values.map((value) => zod_1.z.literal(value));
return zod_1.z.union(zodLiterals);
};
function createUnionSchema(values) {
if (values.length === 0) {
return zod_1.z.never();
}
if (values.length === 1) {
return zod_1.z.literal(values[0]);
}
return createUnion(values);
}
exports.default = createUnionSchema;
const validateJSONPath = (jsonPath) => {
// account for embedded context variables
if (const_1.CONTEXT_PARAM_REGEXP.test(jsonPath)) {
// skip validation
return true;
}
try {
jsonpath_1.JSONPath.parse(jsonPath);
return true;
}
catch (error) {
return false;
}
};
exports.jsonPathSchema = zod_1.z
.string()
.refine((val) => validateJSONPath(val), {
message: 'Invalid JSONPath expression',
})
.describe('A string containing either a valid JSON Path Expression, or a Context Parameter.');
const validateHttpsURL = (url) => {
return URL.canParse(url) && url.startsWith('https://');
};
// Use our own URL refinement check due to https://github.com/colinhacks/zod/issues/2236
exports.httpsURLSchema = zod_1.z
.string()
.url()
.refine((url) => validateHttpsURL(url), {
message: 'Invalid URL',
});
//# sourceMappingURL=common.js.map