npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shopify/app-bridge-validate

v1.0.7

Published

App Bridge Validate is a middleware that validates action-instantiation props and payloads of dispatched actions. It can provide helpful error messages during development. As a separate package from [app-bridge]('../app-bridge'), this utility should be om

Downloads

91

Readme

@shopify/app-bridge-validate

App Bridge Validate is a middleware that validates action-instantiation props and payloads of dispatched actions. It can provide helpful error messages during development. As a separate package from app-bridge, this utility should be omitted in production to reduce file size.

Usage

To use the validator in an app, initialize your app with createAppWrapper instead of createApp. Using createAppWrapper allows you to pass in optional middlewares:

import {createAppWrapper, ClientApplication} from '@shopify/app-bridge';
import validatorMiddleware from '@shopify/app-bridge-validate';

const app = createAppWrapper(window.top, window.location.origin, [validatorMiddleware])({
  apiKey: 'API_KEY_FROM_PARTNER_DASH',
  shopOrigin: 'testshop.myshopify.io',
});

After the validator is set up, it validates action instantiation and dispatch against rules defined in the actions directory. Invalid actions will throw an error.

Validation in App Bridge Playground

The App Bridge Playground is set up to use the validator. To try it out, follow the setup steps from the playground package and then view the app with a shop.

Props validation

Edit code samples from any section of the playground app so that it creates an error. For example:

{
  message: "Unicorn",
  duration: '1234', // duration should be a positive integer
  isError: false,
}

Using the code above to instantiate a Toast action will throw an error. Details of the error can be found in the browser's debug console.

Dispatch validation

In the Any Action section of the playground, attempt to dispatch an action with an invalid payload. For example, try to dispatch the invalid Toast action below:

{
  "type": "APP::TOAST::SHOW",
  "group": "Toast",
  "payload": {
    "id": "123",
    "message": 1234 // message should be a string
  },
  "version": "1.0.0"
}

The action should result in an error with a message property that shows the path to where the error occurred:

{
  "error": {
    "action": {
      "type": "APP::TOAST::SHOW",
      "group": "Toast",
      "payload": {
        "id": "123",
        "message": 1234
      },
      "version": "1.0.0"
    },
    "message": "`type_error_expected_string` thrown for path: ['payload']['message'] and value: `1234` | `type_error_expected_integer` thrown for path: ['payload']['duration'] and value: `undefined`",
    "type": "APP::ERROR::INVALID_PAYLOAD",
    "id": "123"
  }
}

Creating validation rules

New actions and action groups in app-bridge should be accompanied by validation rules. Add new validation rules to a TS file with the name of the action.

The following example adds validation rules to toast.ts for the the Toast action:

import {MetaAction} from '@shopify/app-bridge/actions';
import {ActionType} from '@shopify/app-bridge/actions/Toast';
import {validate, ValidationError} from '@shopify/app-bridge-validate/type-validate';
import {createActionValidator} from '@shopify/app-bridge-validate/utils';

export const toastSchema = matchesObject({
  message: matchesString(),
  duration: matchesPositiveInteger(),
  isError: makeOptional(matchesBoolean()),
});

export function validateProps(props: Indexable) {
  return validate(props, toastSchema);
}

export function validateAction(action: MetaAction): ValidationError[] | undefined {
  switch (action.type) {
    case ActionType.SHOW:
      return validate(action, createActionValidator(ActionType, toastSchema, true));
    default:
      return validate(action, createActionValidator(ActionType));
  }
}