1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | var actions_1 = require("../../actions");
|
15 | var type_validate_1 = require("../type-validate");
|
16 | var utils_1 = require("../utils");
|
17 | function matchesAbsolutePath(value) {
|
18 | return value.match('^https?://')
|
19 | ? undefined
|
20 | : [
|
21 | {
|
22 | value: value,
|
23 | error: 'invalid_absolute_url',
|
24 | message: 'expected string to start with `https://` or `http://`',
|
25 | },
|
26 | ];
|
27 | }
|
28 | function getSectionSchema(payload) {
|
29 | var isProductVariant = payload &&
|
30 | payload.section &&
|
31 | payload.section.resource &&
|
32 | payload.section.name === actions_1.Redirect.ResourceType.Product;
|
33 | var resourceSchema = {
|
34 | create: type_validate_1.makeOptional(type_validate_1.matchesBoolean()),
|
35 | id: type_validate_1.makeOptional(type_validate_1.matchesString()),
|
36 | };
|
37 | var productVariantSchema = __assign({}, resourceSchema, { variant: type_validate_1.makeOptional(type_validate_1.matchesObject(resourceSchema)) });
|
38 | return type_validate_1.matchesObject({
|
39 | section: type_validate_1.matchesObject({
|
40 | name: type_validate_1.matchesEnum(actions_1.Redirect.ResourceType),
|
41 | resource: type_validate_1.makeOptional(type_validate_1.matchesObject(isProductVariant ? productVariantSchema : resourceSchema)),
|
42 | }),
|
43 | });
|
44 | }
|
45 | function validateAction(action) {
|
46 | var newContextSchema = type_validate_1.matchesObject({ newContext: type_validate_1.makeOptional(type_validate_1.matchesBoolean()) });
|
47 | var actionType = actions_1.Redirect.ActionType;
|
48 | var schema;
|
49 | switch (action.type) {
|
50 | case actions_1.History.ActionType.PUSH:
|
51 | case actions_1.History.ActionType.REPLACE:
|
52 | actionType = actions_1.History.ActionType;
|
53 | schema = utils_1.relativePathSchema;
|
54 | break;
|
55 | case actions_1.Redirect.ActionType.APP:
|
56 | schema = utils_1.relativePathSchema;
|
57 | break;
|
58 | case actions_1.Redirect.ActionType.REMOTE:
|
59 | schema = type_validate_1.composeSchemas(type_validate_1.matchesObject({
|
60 | url: type_validate_1.composeSchemas(type_validate_1.matchesString(), function (value) { return matchesAbsolutePath(value); }),
|
61 | }), newContextSchema);
|
62 | break;
|
63 | case actions_1.Redirect.ActionType.ADMIN_PATH:
|
64 | schema = type_validate_1.composeSchemas(utils_1.relativePathSchema, newContextSchema);
|
65 | break;
|
66 | case actions_1.Redirect.ActionType.ADMIN_SECTION:
|
67 | schema = type_validate_1.composeSchemas(getSectionSchema(action.payload), newContextSchema);
|
68 | break;
|
69 | }
|
70 | return type_validate_1.validate(action, utils_1.createActionValidator(actionType, schema));
|
71 | }
|
72 | exports.validateAction = validateAction;
|