{"id":17921856,"url":"https://github.com/zth/flow-enum-validator","last_synced_at":"2025-04-03T11:13:15.774Z","repository":{"id":57238701,"uuid":"145229852","full_name":"zth/flow-enum-validator","owner":"zth","description":"Simple type safe validation of unknown strings to enums using Flow.","archived":false,"fork":false,"pushed_at":"2018-08-18T15:15:47.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-09T04:35:34.359Z","etag":null,"topics":["enum","flow","flowtype","javascript","typing"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-18T15:10:37.000Z","updated_at":"2019-01-09T15:34:08.000Z","dependencies_parsed_at":"2022-08-26T15:12:02.157Z","dependency_job_id":null,"html_url":"https://github.com/zth/flow-enum-validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Fflow-enum-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Fflow-enum-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Fflow-enum-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Fflow-enum-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zth","download_url":"https://codeload.github.com/zth/flow-enum-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246989747,"owners_count":20865331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","flow","flowtype","javascript","typing"],"created_at":"2024-10-28T20:36:26.995Z","updated_at":"2025-04-03T11:13:15.749Z","avatar_url":"https://github.com/zth.png","language":"JavaScript","readme":"# flow-enum-validator\n\nA very small and simple library to help validate unknown strings to enums with Flow.\n\n## Installation\n\n```\nyarn add flow-enum-validator\n```\n\n## Usage\n\nThe main use for this library is mapping unknown strings to allowed values from an enum in a type safe way using Flow.\nCheck the examples out below.\n\n### Basic example\n\n```javascript\nimport { createEnumValidator, validateEnum } from 'flow-enum-validator';\n\n// The enum strings are derived from the _keys_ of the provided object.\nconst myEnumObj = Object.freeze({\n  // Remember to freeze the provided object so Flow knows it cannot change\n  FIRST_ENUM_VALUE: null, // Values can be anything, the validator only looks at the keys\n  SECOND_ENUM_VALUE: null\n});\n\nconst validateString = createEnumValidator(myEnumObj);\n// You can create a validator function for easy reuse.\n// This returns a function with the signature (checkThisValue: string) =\u003e ?$Keys\u003ctypeof myEnumObj\u003e\n// In this specific case, it means that all function calls below return the type 'FIRST_ENUM_VALUE' | 'SECOND_ENUM_VALUE' | null | void\n\nvalidateString('SOME_VALUE_TO_VALIDATE_HERE');\n// This would return void\n\nvalidateString('FIRST_ENUM_VALUE');\n// This would return FIRST_ENUM_VALUE\n\nvalidateEnum(myEnumObj, 'FIRST_ENUM_VALUE');\n// You can also use validateEnum directly with an enum object and value if you don't want a validator function.\n// This would return FIRST_ENUM_VALUE\n```\n\n### Mapping an unknown input to an enum value in a type safe way (in React, but not React specific)\n\n```javascript\nimport { createEnumValidator } from 'flow-enum-validator';\n\nconst ALLOWED_KEYS = Object.freeze({\n  A: null,\n  B: null,\n  C: null\n});\n\ntype AllowedKeysType = $Keys\u003ctypeof ALLOWED_KEYS\u003e; // 'A' | 'B' | 'C';\n\nconst keyValidator = createEnumValidator(ALLOWED_KEYS);\n\n...\n\ntype State = {\n  pressedKey: AllowedKeysType\n}\n\n// Handling input from a \u003cinput type=\"text\" onChange={this.handleInputChange} value={this.state.pressedKey} /\u003e\nhandleInputChange = (event: SyntheticEvent\u003cHTMLInputElement\u003e) =\u003e {\n  const key = keyValidator(event.currentTarget.value); // Variable key is now 'A' | 'B' | 'C' | null | void\n\n  if (key) {\n    // key is now 'A' | 'B' | 'C'\n    this.setState({\n      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\n    })\n  }\n}\n```\n\n### Longer, contrived example and explanation\n\n```javascript\nimport { createEnumValidator } from 'flow-enum-validator';\n\nconst myEnumObj = Object.freeze({\n  FIRST_ENUM_VALUE: null,\n  SECOND_ENUM_VALUE: null,\n  THIRD_ENUM_VALUE: null\n});\n\nconst validateString = createEnumValidator(myEnumObj);\n\n// Here, we take Flow all the way down to a single possible value through a series of type refinements\n\nconst someRandomString = getSomeRandomString();\n// Flow naturally has no clue what this is before the function is actually run in your program,\n// but imagine it returns 'FIRST_ENUM_VALUE' in this example. Lets follow Flow!\n\nconst validatedVal = validateString(someRandomString);\n// 'FIRST_ENUM_VALUE' | 'SECOND_ENUM_VALUE' | 'THIRD_ENUM_VALUE' | null | void\n\nif (validatedVal) {\n  // 'FIRST_ENUM_VALUE' | 'SECOND_ENUM_VALUE' | 'THIRD_ENUM_VALUE'\n\n  if (validatedVal !== 'SECOND_ENUM_VALUE') {\n    // 'FIRST_ENUM_VALUE' | 'THIRD_ENUM_VALUE'\n    if (validatedVal !== 'THIRD_ENUM_VALUE') {\n      // 'FIRST_ENUM_VALUE' is the only possible value left, and Flow understands that. Therefore, this generates no Flow errors:\n      (validatedVal: 'FIRST_ENUM_VALUE');\n    }\n  }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzth%2Fflow-enum-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzth%2Fflow-enum-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzth%2Fflow-enum-validator/lists"}