1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var type_validate_1 = require("./type-validate");
|
4 | function 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 | }
|
19 | exports.createActionValidator = createActionValidator;
|
20 | function 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 | }
|
29 | exports.actionMessage = actionMessage;
|
30 | function isValidRelativePath(path) {
|
31 | return typeof path === 'string' && (path === '' || path.startsWith('/'));
|
32 | }
|
33 | exports.isValidRelativePath = isValidRelativePath;
|
34 | exports.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 | });
|