UNPKG

2.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var type_validate_1 = require("./type-validate");
4function createActionValidator(type, payloadSchema, payloadRequired, idRequired) {
5 if (payloadSchema === void 0) { payloadSchema = undefined; }
6 if (payloadRequired === void 0) { payloadRequired = false; }
7 if (idRequired === void 0) { idRequired = false; }
8 var idSchema = type_validate_1.matchesObject({
9 id: idRequired ? type_validate_1.matchesString() : type_validate_1.makeOptional(type_validate_1.matchesString()),
10 });
11 var schema = payloadSchema ? type_validate_1.composeSchemas(idSchema, payloadSchema) : idSchema;
12 return type_validate_1.matchesObject({
13 type: type_validate_1.matchesEnum(type, {
14 message: function (_, val) { return "The action type `" + val + "` is invalid or unsupported"; },
15 }),
16 payload: payloadRequired ? schema : type_validate_1.makeOptional(schema),
17 });
18}
19exports.createActionValidator = createActionValidator;
20function actionMessage(errors) {
21 return errors
22 .map(function (err) {
23 var path = err.path, error = err.error, message = err.message, value = err.value;
24 var valueStr = typeof value === 'object' ? JSON.stringify(value) : value;
25 return "`" + error + "` thrown for" + (path ? " path: " + path + " and" : '') + " value: `" + valueStr + "`" + (message ? " with message: " + message : '');
26 })
27 .join(' | ');
28}
29exports.actionMessage = actionMessage;
30function isValidRelativePath(path) {
31 return typeof path === 'string' && (path === '' || path.startsWith('/'));
32}
33exports.isValidRelativePath = isValidRelativePath;
34exports.relativePathSchema = type_validate_1.matchesObject({
35 path: type_validate_1.composeSchemas(type_validate_1.matchesString(), function (value) {
36 return isValidRelativePath(value)
37 ? undefined
38 : [{ error: 'invalid_relative_path', value: value, message: 'expected string to start with `/`' }];
39 }),
40});