@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
26 lines (23 loc) • 1.16 kB
JavaScript
import { matchesObject, matchesEnum, makeOptional, composeSchemas, matchesString } from './type-validate.js';
function createActionValidator(type, payloadSchema = undefined, payloadRequired = false, idRequired = false) {
const idSchema = matchesObject({
id: idRequired ? matchesString() : makeOptional(matchesString()),
});
const schema = payloadSchema ? composeSchemas(idSchema, payloadSchema) : idSchema;
return matchesObject({
type: matchesEnum(type, {
message: (_, val) => `The action type \`${val}\` is invalid or unsupported`,
}),
payload: payloadRequired ? schema : makeOptional(schema),
});
}
function isValidRelativePath(path) {
return typeof path === 'string' && (path === '' || path.startsWith('/'));
}
const relativeUrlSchema = composeSchemas(matchesString(), (value) => isValidRelativePath(value)
? undefined
: [{ error: 'invalid_relative_path', value, message: 'expected string to start with `/`' }]);
const relativePathSchema = matchesObject({
path: relativeUrlSchema,
});
export { createActionValidator, isValidRelativePath, relativePathSchema, relativeUrlSchema };