---
name: svelte-store-validator
description: "Builds a writable Svelte store that refuses invalid data: every set and update runs your field rules, errors come back in the style you choose, and the generated test cases prove the rules hold. Use when a Svelte writable store must reject bad data on every set or update, with field rules enforced inside the store rather than in each component. Do not use for form-level validation in a component, or schema validation with an external library such as Zod. Related topics: coding, svelte."
---

# Svelte Store Validator

Builds a writable Svelte store that refuses invalid data: every set and update runs your field rules, errors come back in the style you choose, and the generated test cases prove the rules hold.

## When to use

Use this skill when a Svelte writable store must reject bad data on every set or update, with field rules enforced inside the store rather than in each component. Do not use it for form-level validation in a component, or schema validation with an external library such as Zod.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Write one validator function per field from the rules and a validate function that collects every failure with a readable message.
3. Wrap a writable so subscribe passes through unchanged and set and update run validate before storing; no path can store an invalid value.
4. Implement the chosen error handling in one place (throw, result object, or log and keep) so callers see one consistent contract.
5. Infer and export the value type from the JSON example when TypeScript is requested, matching the export style.
6. Append the requested test cases as comments, half passing and half failing, each failing case naming the field and rule it breaks.

## Inputs

- **Store name** (free text, e.g. "profileStore")
- **Data shape as a JSON example** (longer free text)
- **Validation rules** (longer free text)
- **Error handling** (choose one: Throw, Return a result object, Log and keep the last valid value)
- **Include TypeScript types** (choose one: Yes, No)
- **Export style** (choose one: default, named)
- **Number of test cases to generate** (number from 0 to 10)

## Output

One fenced code block with the complete store, its types and export in the chosen style, and the test cases as trailing comments; nothing outside the block.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Svelte Store Validator

Build a validating writable store from the rules below.

- Store name: {{Store name}}
- Data shape as a JSON example: {{Data shape as a JSON example}}
- Validation rules: {{Validation rules}}
- Error handling: {{Error handling}}
- Include TypeScript types: {{Include TypeScript types}}
- Export style: {{Export style}}
- Number of test cases to generate: {{Number of test cases to generate}}

Design the store as a custom store that wraps a writable: expose subscribe unchanged, and wrap set and update so that every incoming value is validated before it is stored. Write one validator function per field from the rules, then a validate(value) that runs them all and collects every failing field with a readable message, rather than stopping at the first. Apply the chosen error handling in one place: throw a single Error carrying the message list, return an object with ok and errors fields, or log the errors and leave the store untouched. Infer types from the JSON example when TypeScript is requested, and export the value type alongside the store.

Generate the requested number of test cases as comments at the bottom: half valid inputs that must pass, half invalid ones that name the field and rule they break. Note any rule that cannot be checked at runtime from the given shape and say what extra information it needs.

### Output
Return the complete store code in one fenced TypeScript or JavaScript block, including any interfaces and the export statement in the chosen style, with the test cases as trailing comments.

## Pitfalls

- Validating only in set and forgetting update, which lets a callback write an invalid value straight through.
- Stopping at the first failing field, so users fix one error and hit the next on the following submit.
- Throwing inside a store subscriber callback, which surfaces as an unhandled error far from the write that caused it.
