@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
98 lines (95 loc) • 3.85 kB
JavaScript
import { Action, ALL_RESOURCE_VERTICAL_ALIGNMENT, ALL_BADGE_PROGRESSES, ALL_BADGE_STATUSES, ALL_MEDIA_KINDS } from '../../actions/Picker/index.js';
import { validate, matchesObject, matchesArray, makeOptional, matchesString, matchesBoolean, matchesPositiveInteger, matchesEnum } from '../type-validate.js';
import { createActionValidator } from '../utils.js';
const resourceBadge = matchesObject({
content: matchesString(),
id: matchesString(),
progress: matchesEnum(ALL_BADGE_PROGRESSES),
status: matchesEnum(ALL_BADGE_STATUSES),
});
const resourceMedia = matchesObject({
accessibilityLabel: makeOptional(matchesString()),
alt: makeOptional(matchesString()),
initials: makeOptional(matchesString()),
kind: makeOptional(matchesEnum(ALL_MEDIA_KINDS)),
name: makeOptional(matchesString()),
source: matchesString(),
});
const sharedResourceSchema = {
accessibilityLabel: makeOptional(matchesString()),
badges: makeOptional(matchesArray(resourceBadge)),
disabled: makeOptional(matchesBoolean()),
id: matchesString(),
loading: makeOptional(matchesBoolean()),
media: makeOptional(resourceMedia),
name: makeOptional(matchesString()),
caption: makeOptional(matchesString()),
selectable: makeOptional(matchesBoolean()),
};
const resourceOption = matchesObject({
...sharedResourceSchema,
});
const resourceSelectionSchema = matchesArray(matchesObject({
...sharedResourceSchema,
options: makeOptional(matchesArray(resourceOption)),
}));
const resourceName = matchesObject({
plural: matchesString(),
singular: matchesString(),
});
const pickerOptions = matchesObject({
canLoadMore: makeOptional(matchesBoolean()),
emptySearchLabel: makeOptional(matchesObject({
title: matchesString(),
description: matchesString(),
withIllustration: matchesBoolean(),
})),
items: makeOptional(resourceSelectionSchema),
loading: makeOptional(matchesBoolean()),
loadingMore: makeOptional(matchesBoolean()),
maxSelectable: makeOptional(matchesPositiveInteger()),
primaryActionLabel: makeOptional(matchesString()),
searchQuery: makeOptional(matchesString()),
searchQueryPlaceholder: makeOptional(matchesString()),
secondaryActionLabel: makeOptional(matchesString()),
selectedItems: makeOptional(matchesArray(matchesObject(sharedResourceSchema))),
title: makeOptional(matchesString()),
verticalAlignment: makeOptional(matchesEnum(ALL_RESOURCE_VERTICAL_ALIGNMENT)),
allowEmptySelection: makeOptional(matchesBoolean()),
resourceName: makeOptional(resourceName),
});
const pickerActionSchema = matchesObject({
options: makeOptional(resourceSelectionSchema),
});
const selectionSchema = matchesObject({
selectedItems: resourceSelectionSchema,
});
const searchSchema = matchesObject({
searchQuery: makeOptional(matchesString()),
});
/**
* @unstable This API may be updated without warning in the future
*/
function validateProps(props) {
return validate(props, pickerOptions);
}
/**
* @unstable This API may be updated without warning in the future
*/
function validateAction(action) {
switch (action.type) {
case Action.UPDATE:
case Action.OPEN:
return validate(action, createActionValidator(Action, pickerActionSchema, false, true));
case Action.SELECT:
return validate(action, createActionValidator(Action, selectionSchema, true, true));
case Action.SEARCH:
return validate(action, createActionValidator(Action, searchSchema, true, true));
case Action.CANCEL:
case Action.LOAD_MORE:
return validate(action, createActionValidator(Action));
default:
return validate(action, createActionValidator(Action));
}
}
export { Action, validateAction, validateProps };