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

@mfgomess/alpine-zod-validation

v1.1.7

Published

A simple form validation plugin for Alpine.js using Zod

Downloads

2,607

Readme

Alpine.js Zod Validation

Is an integration between Alpine.js and Zod for form validation. This library allows you to define validation schemas using Zod and apply them directly in your Alpine.js components with ease.

Table of Contents

Installation

As a npm module + bundler

Install the package via npm:

$ npm i @mfgomess/alpine-zod-validation

Import the package in your project and start using it:

import Alpine from 'alpinejs';
import {zValidation} from 'alpine-zod-validation';

window.Alpine = Alpine;
Alpine.plugin(zValidation);
Alpine.start();

Use it from CDN


<script type="module" defer src="https://unpkg.com/@mfgomess/alpine-zod-validation/dist/cdn.js"/>
<script src="//unpkg.com/alpinejs" defer></script>

Usage

To use validation you should apply the 'x-zvalidation' directive to your alpinejs component, and define a 'zSchema' property with your Zod schema, then you can use the validation methods provided by this plugin.

Directives

  • x-zvalidation: Directive to apply validation to the component.
  • .listen: Modifier to listen to a specific event in child elements.
  • .reactive: To validate on input event after the first validation error occurred.
  • .entangled: To sync livewire validation errors with entangled properties of the component.

Magic Properties

  • $z: Magic method of Zod object instance.

<form
    @submit.prevent="save"
    x-zvalidation.entangled.reactive.listen="change"
    x-data="{
        name: @entangle('clientData.name'), // Livewire entangled property
        zSchema: $z.object({ 
            name: $z.string().min(3), 
            email: $z.string().email() 
        }),
        save() {
            if(this.zValidate()) {
                console.log('Form is valid');
            }
        }
    }"
>
    <input
        type="text"
        x-model="name"
        x-bind:class="{ 
            'errored': zIsInvalid('name'), 
            'success': zIsValid('name') 
        }"
    />
    <span x-show="zIsInvalid('name')" x-text="zFirstErrorFor('name')"></span>
    <button type="submit">Submit</button>
</form>

Features

On-event validation

You can add a .listen modifier and an event to the x-validation directive, it will listen to this occurred in child html elements with x-model directive and validate only this for field property.


<form
    x-zvalidation.listen="input"
    x-data="{ name: '', zSchema: $z.object({ name: $z.string().min(3) }) }"
>
    <!-- The field name will be validated when the input event is triggered -->
    <input type="text" x-model="name"/>
</form>

Reactive validation

Once you use a .listen modifier to validate on change or blur events, it will validate the field only when you change the value, and it will keep the error status until the user trigger the change event again. With .reactive modifier it will modify the validation to input event after the first validation error occurred in the field.


<form
    x-zvalidation.listen.reactive="input"
    x-data="{ name: '', zSchema: $z.object({ name: $z.string().min(3) }) }"
>
    <!-- The field name will be validated when the input event is triggered -->
    <input type="text" x-model="name"/>
</form>

Livewire entangled validation

Once you add a .entangled modifier to the x-validation directive, it will sync the validation errors with the entangled properties of the component.

If Livewire return validation errors for entangled properties, they will be synced with the x-zvalidation errors and the field will be marked as invalid.

Note: It will already map the errors to the entangled properties, so you don't need to keep the livewire and alpinejs properties with same keys.


<form
    x-zvalidation.entangled.listen="input"
    x-data="{ 
        name: @entangle('name'),
        phone: @entangle('client.phone'), 
        zSchema: $z.object({ 
            name: $z.string().min(3),
            phone: $z.string().min(10)
        }) 
    }"
>
    <!-- The field name will be validated when the input event is triggered -->
    <input type="text" x-model="name"/>

    <!-- The field phone will be validated when the input event is triggered -->
    <input type="text" x-model="phone"/>

    <!-- Once you validate the form with Livewire "save" method, the errors will be synced with Alpine.js if exists -->
    <button
        x-bind:disabled="zHasErrors()"
        wire:click.prevent="save"
    >Submit
    </button>
</form>

$z magic method

The $z magic method is just a shortcut to the Zod object, you can use it to define your validation schema in a more readable way.

Check Zod documentation for more information about Zod.

x-zvalidation directive methods added to the component

Once you add the x-zvalidation directive to your component, it will add some methods to the component instance to help you with the validation.

| Method | Return Type | Description | |-------------------------|-------------------|---------------------------------------------------------------------------------------------------| | zIsValid('field') | boolean | Checks if a specific field is valid, returns a boolean. (Triggered only after the field is dirty) | | zIsInvalid('field') | boolean | Checks if a specific field is invalid, returns a boolean. | | zFirstErrorFor('field') | ?string | Gets the error message of a specific field, returns a string. | | zGetErrorsFor('field') | string[] | Gets all error messages for a specific field, returns an array. | | zAllErrors() | {field: string[]} | Gets all error messages, returns an object. | | zHasErrors() | boolean | Checks if the form has any errors, returns a boolean. | | zAllSuccesses() | {field: boolean} | Checks if the form has any successes, returns an object. | | zValidate() | boolean | Checks if the form is valid or not, returns a boolean. | | zValidateOnly('field') | boolean | Checks if a specific field is valid or not, returns a boolean. | | zReset() | void | Resets the form validation state. |

Contributing

Contributions, issues, and feature requests are welcome.

License

MIT