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

aovi-svelte

v0.2.5

Published

Easy using Aovi in your Svelte apps.

Downloads

31

Readme

aovi-svelte

Easy using Aovi in your Svelte apps

See in REPL

Features

  • Based on Svelte's store feature
  • One store may be used for different validation chains in same component(s)
  • Instant checkers for any property in the store
  • Easy to use with aovi validator on the server side

Common example

<script>
    import {aoviSvelte} from 'aovi-svelte';

    // Create the aoviSvelte store containing object of values to be validated
    const form = aoviSvelte({
        user: '',
        password: ''
    });

    function doSubmit(){
        // Run aovi validators
        form.aovi
            .check('user')
                .required()
                .match(/^[a-z]+$/)
            .check('password')
                .required()
                .minLength(4)
            .end // You must always use .end operator

        if($form.valid){ // when aovi checks was successful, do thing
            fetch(...)
        }
    }
</script>

User: 
<input bind:value={$form.user} class:error={$form.err.user}/>

Password: 
<input bind:value={$form.password} class:error={$form.err.password} />

{#if !$form.valid}
    {#each $form.err.toArray() as error}
        <p>{error}</p>
    {/if}
{/if}

<button on:click={doSubmit}>Submit</button>

Example with aovi validator on the server

Server request handler

// handler for http-request with body_parser.json middleware
const aovi = require('aovi');
function authUser(req,res){
    const checkPassword = password => password === '12345';

    // Validate form object from request body(after body_parser.json
    let result  = aovi(req.body)
        .check('password')
            .required()
            .is(checkPassword,"Wrong password");

    // send responce as JSON string 
    res.end(result.json())
}

Client form

<script>
    import {aoviSvelte} from 'aovi-svelte';

    // Create validation store
    const form = aoviSvelte({
        password: ''
    });

    async function doLogin(){
        // do request to the server
        const responce = await fetch('.../api/auth',{
            method:'POST',
            body:JSON.stringify({password: $form.password}) //OR (form.get()) to send all $form entries
        }); 

        // load validation result from server and update the store
        form.load(await responce.json());
        
        // do some staff if validation was ok
        if($form.valid){
            console.log('User authed!');
        }
    }
</script>

Password: 
<input bind:value={$form.password}/>
{#if !$form.valid}{$form.err.password}{/if}

<button on:click={doLogin}>Login</button>

Checkers

Checkers are special stores derived from any property in the aoviSvelte store. Its subscription returns true or false each time the value changes.

<script>
    import {aoviSvelte} from 'aovi-svelte';
    const form = aoviSvelte({
        password: ''
    });

    // checker will be true if password value is strong password.
    const good_password = form.checker('password',aovi => aovi.minlength(8).match(/[A-Z]/));
</script>

<input bind:value={$form.password}/>
{#if $good_password}
    Your password is strong!
{:else}
    Please enter more than 8 signs and at least 1 capital letter.
{/if}

Custom validators

You can use any aovi custom validator. The best way is to declare it in the your app's root file like App.svelte:

<script>
    ...
    import {use} from 'aovi-svelte';
    import {between} from './my-custom-validators.js';

    ...
    use(between);
</script>

Then you can use this custom validator in the any chain in your other files.

<script>
    import {aoviSvelte} from 'aovi-svelte';
    ...
    form.aovi
        .check('my_number')
            .required()
            .between(0,10);
    ...
</script>

API

aoviSvelte(object_to_validate)

Returns the aoviSvelte store

use(custom_validator)

Globally adds aovi custom validators. Usually called in app's root file like App.svelte.

<store>.aovi

Returns aovi object to call validators. You must finish chain with .end operator, to proceed validation and update store.

<store>.clear()

Clear all errors. Make <$store>.valid true;

<store>.get([property][,property]...)

If no arguments specified, returns an initial object with current values. You can specify what properties should be in object by providing their names as a function arguments.

<store>.error(message,[property])

Add a new error message for the property in the store. If property ommited, add anonymus message, which will be appeared only in <$store>.err.toArray() call. Make <$store>.valid false;

<store>.load(result_array)

Load aovi's result array in the validation store. May be used when you got aovi validation responce from the sever.

<store>.checker(property,func)

Returns checker store, which has value true or false, based on the current value of the property and validation chain from func. The func get aovi object as a first parameter, you must chain validators (except .required and .check ) to this object and return it.

<$store>.valid

Equal true when no validation errors, false when there is at least one error occured.

<$store>.<property>

Current property value.

<$store>.err.<property>

Equal false when no validation error for the property, or text of the error in other case.

<$store>.err.toArray()

Returns all errors messages as array. Returns [] when no errors.