3.0.6 ā€¢ Published 25 days ago

tsurih v3.0.6

Weekly downloads
-
License
MIT
Repository
-
Last release
25 days ago

Tsurih

Svelte form validation library

  • Minimalistic šŸš
  • Type safe šŸ›Ÿ
  • Small size šŸ“¦
  • Ability to use custom validators ā˜˜ļø
  • Strategies šŸŽ–ļø
  • Helper functions šŸ’Ŗ
  • Focused on DX ā¤ļø

Example šŸ‘‡

<script lang="ts">
  import { get } from "svelte/store";
  import { tsurih, debounce, validators } from "tsurih";
  import type { TConfig, TContext } from "tsurih";

  const debounced = debounce(() => {
    return new Promise<boolean>((res) =>
      setTimeout(() => {
        console.log("šŸš‚");
        res(true);
      })
    );
  }, 2000);

  const customValidators = {
    debounce: (message?: string) => {
      return async (ctx: TContext<any, any, TConfig<any, any>>) => {
        const result = await debounced();

        const values = get(ctx.config.values);

        if (!result) {
          // āŒ Error
          return message || "Field is not valid";
        }

        // āœ… Success
        return "";
      };
    },
  };

  const form = tsurih({
      field1: true,
      field2: "",
    })
  .setSchema({
    field1: [validators.required()],
    field2: [
      validators.required(),
      (ctx) => {
        const values = get(ctx.config.values);

        if (!values.field1) {
          // āŒ Error
          return "Field is not valid";
        }

        // āœ… Success
        return "";
      },
      customValidators.debounce(),
    ],
  })
  .setStrategy("blur");

  $: watchOnlyField1 = $values.field1;

  $: {
    watchOnlyField1;
    form.revalidate("field2");
  }

  const { errors, isSubmitting, values } = form;
</script>

<form
  use:form.reg
  on:submit="{form.onSubmit((values) => {
    console.log(values);
  })}"
>
  <input
    type="checkbox"
    bind:checked="{$values.field1}"
    name="{form.fields.field1}"
  />

  {#if $errors.field1}
    <span>{$errors.field1}</span>
  {/if}

  <input bind:value="{$values.field2}" name="{form.fields.field2}" />

  {#if $errors.field2}
    <span>{$errors.field2}</span>
  {/if}

  {#if $isSubmitting}
    Submitting...
  {/if}

  <button type="submit">Submit</button>
</form>

ā˜˜ļø tsurih also provides validators, isString, isNumber, isArray, isObject, debounce utilities

ā˜˜ļø "validators" utility which has standard min, max, required, regexp, optionalMin, optionalRegexp.

ā˜˜ļø An additional validator "optional" is not needed as all default fields are optional. But if you, for example, want min or regexp to be checked only if a value is present, use optionMin or optionalRegexp . If you are writing your own validator which should be optional, then it should have an if(!ctx.value) return "" condition

Author

Vladislav Yemelyanov

Released under the MIT License.

3.0.6

25 days ago

3.0.4

4 months ago

3.0.3

4 months ago

3.0.5

4 months ago

3.0.2

9 months ago

3.0.1

9 months ago

3.0.0

9 months ago

2.0.16

9 months ago

2.0.15

9 months ago

2.0.14

9 months ago

2.0.13

9 months ago

2.0.12

9 months ago

2.0.11

9 months ago

2.0.10

9 months ago

2.0.9

9 months ago

2.0.8

9 months ago

2.0.7

9 months ago

2.0.6

9 months ago

2.0.5

10 months ago

2.0.4

10 months ago

2.0.3

10 months ago

2.0.2

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

0.0.37

10 months ago

0.0.36

10 months ago

0.0.35

10 months ago

0.0.34

10 months ago

0.0.33

10 months ago

0.0.32

10 months ago

0.0.31

10 months ago

0.0.30

10 months ago

0.0.29

10 months ago

0.0.28

10 months ago

0.0.27

10 months ago

0.0.26

10 months ago

0.0.25

10 months ago

0.0.24

10 months ago

0.0.23

10 months ago

0.0.22

10 months ago

0.0.21

10 months ago

0.0.20

10 months ago

0.0.19

10 months ago

0.0.18

10 months ago

0.0.17

10 months ago

0.0.16

10 months ago

0.0.15

10 months ago

0.0.14

10 months ago

0.0.13

10 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago