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

flow-enum-validator

v1.0.0

Published

Utility for validating Flow enum strings in runtime in a type safe way.

Downloads

24

Readme

flow-enum-validator

A very small and simple library to help validate unknown strings to enums with Flow.

Installation

yarn add flow-enum-validator

Usage

The main use for this library is mapping unknown strings to allowed values from an enum in a type safe way using Flow. Check the examples out below.

Basic example

import { createEnumValidator, validateEnum } from 'flow-enum-validator';

// The enum strings are derived from the _keys_ of the provided object.
const myEnumObj = Object.freeze({
  // Remember to freeze the provided object so Flow knows it cannot change
  FIRST_ENUM_VALUE: null, // Values can be anything, the validator only looks at the keys
  SECOND_ENUM_VALUE: null
});

const validateString = createEnumValidator(myEnumObj);
// You can create a validator function for easy reuse.
// This returns a function with the signature (checkThisValue: string) => ?$Keys<typeof myEnumObj>
// In this specific case, it means that all function calls below return the type 'FIRST_ENUM_VALUE' | 'SECOND_ENUM_VALUE' | null | void

validateString('SOME_VALUE_TO_VALIDATE_HERE');
// This would return void

validateString('FIRST_ENUM_VALUE');
// This would return FIRST_ENUM_VALUE

validateEnum(myEnumObj, 'FIRST_ENUM_VALUE');
// You can also use validateEnum directly with an enum object and value if you don't want a validator function.
// This would return FIRST_ENUM_VALUE

Mapping an unknown input to an enum value in a type safe way (in React, but not React specific)

import { createEnumValidator } from 'flow-enum-validator';

const ALLOWED_KEYS = Object.freeze({
  A: null,
  B: null,
  C: null
});

type AllowedKeysType = $Keys<typeof ALLOWED_KEYS>; // 'A' | 'B' | 'C';

const keyValidator = createEnumValidator(ALLOWED_KEYS);

...

type State = {
  pressedKey: AllowedKeysType
}

// Handling input from a <input type="text" onChange={this.handleInputChange} value={this.state.pressedKey} />
handleInputChange = (event: SyntheticEvent<HTMLInputElement>) => {
  const key = keyValidator(event.currentTarget.value); // Variable key is now 'A' | 'B' | 'C' | null | void

  if (key) {
    // key is now 'A' | 'B' | 'C'
    this.setState({
      pressedKey: key // This type checks fine as we've refined our type enough for Flow to know that the possible values match the definition in type State
    })
  }
}

Longer, contrived example and explanation

import { createEnumValidator } from 'flow-enum-validator';

const myEnumObj = Object.freeze({
  FIRST_ENUM_VALUE: null,
  SECOND_ENUM_VALUE: null,
  THIRD_ENUM_VALUE: null
});

const validateString = createEnumValidator(myEnumObj);

// Here, we take Flow all the way down to a single possible value through a series of type refinements

const someRandomString = getSomeRandomString();
// Flow naturally has no clue what this is before the function is actually run in your program,
// but imagine it returns 'FIRST_ENUM_VALUE' in this example. Lets follow Flow!

const validatedVal = validateString(someRandomString);
// 'FIRST_ENUM_VALUE' | 'SECOND_ENUM_VALUE' | 'THIRD_ENUM_VALUE' | null | void

if (validatedVal) {
  // 'FIRST_ENUM_VALUE' | 'SECOND_ENUM_VALUE' | 'THIRD_ENUM_VALUE'

  if (validatedVal !== 'SECOND_ENUM_VALUE') {
    // 'FIRST_ENUM_VALUE' | 'THIRD_ENUM_VALUE'
    if (validatedVal !== 'THIRD_ENUM_VALUE') {
      // 'FIRST_ENUM_VALUE' is the only possible value left, and Flow understands that. Therefore, this generates no Flow errors:
      (validatedVal: 'FIRST_ENUM_VALUE');
    }
  }
}