Skip to content
tezvyn:

How would you build a reusable Svelte validation action?

Source: svelte.devMediumHow cards are made

How would you build a reusable Svelte validation action?

Tests Svelte action lifecycles versus inline logic. Strong answers describe a node function that attaches input or blur listeners, toggles classes or ARIA directly, returns update and destroy, and cite reusability.

What's really being asked

This question probes whether you understand Svelte actions as element-level lifecycle hooks rather than generic utility functions. Interviewers want to see that you know actions run after the element mounts to the DOM, receive the actual node as their first argument, and are the idiomatic escape hatch when you need imperative DOM behavior inside a declarative framework.

The full answer

First, the signature. The action is a function that takes the HTMLInputElement as its first argument and an optional parameters object. It attaches event listeners for input or blur events. Second, direct DOM manipulation. Instead of changing Svelte component state, the action toggles CSS classes like invalid or sets ARIA attributes like aria-invalid directly on the node. Third, the return object. A well-built action returns an object containing an update method so Svelte can call it when parameters change, and a destroy method that removes event listeners and resets attributes when the element unmounts. Fourth, the advantages. Actions keep validation logic reusable across any input in any component, they prevent component scripts from becoming cluttered with imperative DOM code, they co-locate side effects with the element they affect, and they guarantee cleanup because Svelte invokes destroy automatically.

The mistakes people make

A red flag is treating the action like a Svelte store or a reactive statement. Another is mutating $state or other component state from inside the action; actions should touch the DOM, not the component's reactive graph. Forgetting to return a destroy handler signals you do not understand the lifecycle contract and raises concerns about memory leaks. Finally, suggesting the action returns HTML or renders content shows confusion with snippets or components.

What usually comes next

The interviewer might ask how you would pass dynamic validation rules to the action, which leads to the update hook. They might ask how to compose multiple actions on one element, or how to test validation logic independently of Svelte's component renderer. A senior candidate may also be asked how to expose validation state back to the parent component without breaking the action pattern, perhaps using a callback parameter or a custom event dispatched from the node.

A concrete example

You could write a function called validate that takes node and options. Inside, define a handler that runs regex or required checks against node.value. If invalid, call node.classList.add and node.setAttribute. Register the listener with node.addEventListener. Return an object with update to reconfigure rules when options change, and destroy to call removeEventListener and classList.remove. The component usage is simply input use:validate={rules}. This keeps the component template declarative while the imperative validation lives in a single portable module.

Interview question

When building a reusable Svelte validation action, which approach satisfies the lifecycle contract and keeps the logic portable?

  • a.Accept the DOM node, attach listeners, toggle classes or ARIA directly, and return update and destroy methods.Correct
  • b.Mutate component $state from inside the listener and return a cleanup function that resets the store.
  • c.Subscribe to a writable store and apply classes through a reactive derived statement.
  • d.Return an HTML snippet that replaces the input content when validation fails.
Why?

A Svelte action receives the mounted node and must return an object with update and destroy to handle parameter changes and automatic cleanup. Distractor A is tempting because stores are idiomatic in Svelte, but actions should perform imperative DOM manipulation directly rather than coupling to the component's reactive state graph.

Just read this? Test yourself on what you have been reading.

Read the original → svelte.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on svelte — each one lists the topics its interview covers.

See open roles