Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Functions

Functions

schema

  • schema<T>(store: T | Readable<T> | Writable<T>, json_schema: JsonSchema, start?: IStoreStartStopNotifier<T>): Readable<T> | Writable<T>
  • Returns a Readable / Writable Svelte Store, that validates values set to the Store, and retrieved via subscriptions, against a JSON Schema

    NOTE: If a non-Store value is passed as store, then it will be wrapped in a Writable Store

    import {schema} from "svelte-commons/lib/stores/shared";
    
    // Here, we're defining our JSONSchema that will validate our I/O
    //
    // Check out for more information: https://json-schema.org/
    const person_schema = {
        $id: "https://example.com/person.schema.json",
        $schema: "http://json-schema.org/draft-07/schema#",
        title: "Person",
        type: "object",
        properties: {
            firstName: {
                type: "string",
                description: "The person's first name."
            },
    
            lastName: {
                type: "string",
                description: "The person's last name."
            },
    
            age: {
                description: "Age in years which must be equal to or greater than zero.",
                type: "integer",
                minimum: 0
            }
        }
    };
    
    // We need to seed our with an initial value, so here's a sample person
    const initial_person = {
        firstName: "John",
        lastName: "Smith",
        age: 32
    };
    
    // Now we just pass our initial data and our JSON Schema in
    const store = schema(initial_person, person_schema);
    
    // To demonstrate changes, we need to log each change
    store.subscribe(console.log); // logs: `{firstName: "John", lastName: "Smith", age: 32}`
    
    // Here, we're copying our initial "Person", then updating its age to `21`
    store.set({...initial_person, age: 21}); // logs: `{firstName: "John", lastName: "Smith", age: 21}`
    
    // Same as above, but using a negative `.age` to have an exception thrown
    store.set({...initial_person, age: -60}); // throws exception: `Uncaught TypeError: bad change 'Person/age' to Schema Store (Value -60 is less than minimum 0)`

    Type parameters

    Parameters

    Returns Readable<T> | Writable<T>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc