1.2.7 • Published 10 months ago

@larsbaalmans/delta-client-schema v1.2.7

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

delta-client-schema

This library is part of the Delta Client utility packages. It provides a utility to convert abstract building blocks and their values to usable data structures.

Usage

Defining a schema looks as follows consists of a root object and nested fields. For example for a building block that has a welcome message, the schema would look like this:

import { s } from "@larsbaalmans/delta-client-schema";

const heroBuildingBlock = s.object({
    title: s.primitive(c('root', 'title')),
    description: s.cast(c('root', 'description')),
    images: s.array(s.asset())
})

The schema can then be used to convert the abstract building block to a usable data structure:

import { p as parser } from "@larsbaalmans/delta-client-schema";

const heroBuildingBlock = parser.parse(schema, buildingBlockFieldFulfillments);

Yields:

const heroBuildingBlock = {
    title: "Hero Title",
    description: {
        type: 'root',
        children: [
            {
                type: 'text',
                value: 'Welcome to our website'
            }
        ]
    },
    images: [
        {
            fileName: "first image",
            url: 'https://example.com/image.jpg',
        }
    ]
}

API

Object

s.object({
    // shape of the object
})

Array

Default array with one placeholder

s.array(
    s.primitive('value')
)

Array with a limit of the maximum number of items to display

s.array(
    s.primitive('value'), 5 // limit to 5 items
)

Array with an array of placeholders, also uses this a minimum number of items to display

s.array(
    [
        s.primitive('value'),
        s.primitive('value'),
        // other fields
    ]
)

Primitive

string

s.primitive('value')

number

s.primitive(23)

Asset

Accepts a partial Asset object as a placeholder

s.asset({
    fileName: 'first',
    url: 'https://example.com/image.jpg'
})

PathPart

Accepts a partial PathPart object as a placeholder

s.pathPart({
    path: '/blogs',
})

GridPlacement

Accepts a partial GridPlacement object as a placeholder

s.gridPlacement({
    prn: 'prn:grid:123',
    row: 1
})

Cast

Accepts a correct root object to cast the schema to a usable data structure

s.cast({
    type: 'root',
    children: []
})
1.2.7

10 months ago

1.2.6

10 months ago

1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

11 months ago

1.2.1

11 months ago

1.2.0

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago