{"id":13767605,"url":"https://github.com/bigbeno37/zod-localstorage","last_synced_at":"2025-05-02T01:30:37.085Z","repository":{"id":57406398,"uuid":"459445067","full_name":"bigbeno37/zod-localstorage","owner":"bigbeno37","description":"A Typescript library to allow typesafe access to localstorage using schema validation from Zod","archived":false,"fork":false,"pushed_at":"2022-02-18T07:44:49.000Z","size":119,"stargazers_count":6,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-25T14:00:56.429Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bigbeno37.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2022-02-15T05:48:47.000Z","updated_at":"2025-02-25T13:53:52.000Z","dependencies_parsed_at":"2022-09-02T16:35:38.213Z","dependency_job_id":null,"html_url":"https://github.com/bigbeno37/zod-localstorage","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbeno37%2Fzod-localstorage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbeno37%2Fzod-localstorage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbeno37%2Fzod-localstorage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigbeno37%2Fzod-localstorage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bigbeno37","download_url":"https://codeload.github.com/bigbeno37/zod-localstorage/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251969251,"owners_count":21673182,"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":[],"created_at":"2024-08-03T16:01:10.206Z","updated_at":"2025-05-02T01:30:36.741Z","avatar_url":"https://github.com/bigbeno37.png","language":"TypeScript","funding_links":[],"categories":["Other"],"sub_categories":[],"readme":"# zod-localstorage\nA Typescript library to allow typesafe access to localstorage using schema validation from Zod\n\n## Usage\nCreate an object mapping every key to appear in localStorage to a Zod schema:\n```ts\nconst TodoItems = z.array(z.object({\n    name: z.string(),\n    completed: z.boolean()\n}));\n\nconst LocalStorageKeys = {\n    TODO_ITEMS: TodoItems\n};\n```\n\nCreate a `ZodLocalStorage` instance using this object:\n```ts\nconst Storage = new ZodLocalStorage(LocalStorageKeys);\n```\n\nTo retrieve items, first verify that the retrieved item matches the given schema:\n```ts\nconst validationResult = Storage.getItem(\"TODO_ITEMS\");\n\nif (!validationResult.success) {\n    // log error, show notification, etc.\n    return;\n}\n\n// Validation must be successful here\nconst { data } = validationResult; // Will be of type Array\u003c{ name: string, completed: boolean }\u003e\n```\n\nIf you'd prefer a more traditional try-catch approach, use `ZodLocalStorage.getItemOrThrow`:\n```ts\ntry {\n    const validationResult = Storage.getItem(\"TODO_ITEMS\");\n\n    // Validation must be successful here\n    const { data } = validationResult; // Will be of type Array\u003c{ name: string, completed: boolean }\u003e\n} catch (e) {\n    // handle error\n    return;\n}\n```\n\nTo set items, use `ZodLocalStorage.setItem()`. NOTE: There is no need to use JSON.stringify for the value; it will automatically be called on the given value. \n```ts\nconst todoItems = [{ name: \"Write some code\", completed: false }];\n\nStorage.setItem(\"TODO_ITEMS\", todoItems);\n```\n\n## FP (Functional Programming) Module\nFor those who would prefer a more functional approach, `zod-localstorage` also comes with module that is more suited to a functional style, and uses the popular `purify-ts` library. As prior, create an object containing storage keys mapped to Zod schemas:\n\n```ts\nimport {generateAccessor} from \"zod-localstorage/fp\";\n\nconst TodoItems = z.array(z.object({\n    name: z.string(),\n    completed: z.boolean()\n}));\n\nconst LocalStorageKeys = {\n    TODO_ITEMS: TodoItems\n};\n\nconst getKey = generateAccessor(LocalStorageKeys);\n\nconst todoItems = getKey(\"TODO_ITEMS\");\n\ntodoItems.setItem([ { name: \"Learn Haskell\", completed: false } ]); // Sets the \"TODO_ITEMS\" key in localStorage to the given value\n\nconst result = todoItems.getItem(); // Currently of type Either\u003cZodError, Array\u003c{ name: string, completed: boolean}\u003e\n\ntodoItems.clear(); // Clears the todo items from local storage\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigbeno37%2Fzod-localstorage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbigbeno37%2Fzod-localstorage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigbeno37%2Fzod-localstorage/lists"}