UNPKG

@shopify/app-bridge-core

Version:

**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**

61 lines (58 loc) 2.32 kB
import { Action as Action$1 } from '../../actions/Menu/NavigationMenu/index.js'; import { Action } from '../../actions/Menu/ChannelMenu/index.js'; import { validate, composeSchemas, matchesObject, makeOptional, matchesArray, matchesString } from '../type-validate.js'; import { createActionValidator } from '../utils.js'; import { findMatchInEnum } from '../../actions/helper.js'; import { linkActionSchema, linkPropsSchema } from './link.js'; const linkOptionsValidator = matchesObject({ id: matchesString(), options: linkPropsSchema }); function activeLinkError(value) { return [ { error: 'invalid_active_item', value, message: 'expected active item to exist in menu items', }, ]; } function getOptionsSchema(options) { const baseSchema = matchesObject({ items: makeOptional(matchesArray(linkOptionsValidator)), active: makeOptional(linkOptionsValidator), }); const { items, active } = options; if (items && active) { const activeItemSchema = matchesObject({ active: composeSchemas(linkOptionsValidator, (value) => items.find((item) => item.id === value.id) ? undefined : activeLinkError(value)), }); return composeSchemas(baseSchema, activeItemSchema); } return baseSchema; } function getPayloadSchema(payload) { const baseSchema = matchesObject({ items: makeOptional(matchesArray(linkActionSchema)), active: makeOptional(matchesString()), }); const { items, active } = payload; if (items && active) { const activeItemSchema = matchesObject({ active: composeSchemas(matchesString(), (value) => items.find((item) => item.id === value) ? undefined : activeLinkError(value)), }); return composeSchemas(baseSchema, activeItemSchema); } return baseSchema; } function validateProps(props) { const result = validate(props, getOptionsSchema(props)); return result; } function validateAction(action) { let actionType = Action$1; if (findMatchInEnum(Action, action.type)) { actionType = Action; } return validate(action, createActionValidator(actionType, getPayloadSchema(action.payload), true, false)); } export { validateAction, validateProps };