How to use superstruct - 10 common examples

To help you get started, we’ve selected a few superstruct examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ianstormtaylor / superstruct / examples / composing-structs.js View on Github external
import { struct } from 'superstruct'

// Define a `user` struct.
const User = struct({
  id: 'number',
  name: 'string',
})

// Define an `article` struct, composing the user struct in the article's
// `author` property.
const Article = struct({
  id: 'number',
  title: 'string',
  created_at: 'date',
  published_at: 'date?',
  author: User,
})

// Define data to be validated.
const data = {
github ianstormtaylor / superstruct / examples / throwing-errors.js View on Github external
import { struct } from 'superstruct'

// Define a struct to validate with.
const User = struct({
  id: 'number',
  name: 'string',
  email: 'string',
})

// Define data to be validated.
const data = {
  id: 1,
  name: true,
  email: 'jane@example.com',
}

// Validate the data. In this case the `name` property is invalid, so a
// `property_invalid` error will be thrown.
try {
  User(data)
github saojs / kopy / lib / validateConfig.js View on Github external
module.exports = (config, extendConfigSchema) => {
  // TODO: improve prompts and actions validation
  const schema = {
    description: struct.optional('string'),
    prepare: struct.optional('function'),
    prompts: struct.optional(struct.union(['array', 'function'])),
    actions: struct.optional(struct.union(['array', 'function'])),
    completed: struct.optional('function')
  }

  if (typeof extendConfigSchema === 'function') {
    extendConfigSchema(schema, struct, superstruct)
  }

  const res = struct(schema)

  const [error, result] = res.validate(config)
  if (error) {
    throw new KopyError(`Invalid Kopy config: ${error.message}`)
  }
  return result
}
github saojs / kopy / lib / validateConfig.js View on Github external
module.exports = (config, extendConfigSchema) => {
  // TODO: improve prompts and actions validation
  const schema = {
    description: struct.optional('string'),
    prepare: struct.optional('function'),
    prompts: struct.optional(struct.union(['array', 'function'])),
    actions: struct.optional(struct.union(['array', 'function'])),
    completed: struct.optional('function')
  }

  if (typeof extendConfigSchema === 'function') {
    extendConfigSchema(schema, struct, superstruct)
  }

  const res = struct(schema)

  const [error, result] = res.validate(config)
  if (error) {
    throw new KopyError(`Invalid Kopy config: ${error.message}`)
  }
  return result
github libp2p / js-libp2p / src / config.js View on Github external
'use strict'

const { struct, superstruct } = require('superstruct')
const { optional, list } = struct

// Define custom types
const s = superstruct({
  types: {
    transport: value => {
      if (value.length === 0) return 'ERROR_EMPTY'
      value.forEach(i => {
        if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
      })
      return true
    },
    protector: value => {
      if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
      return true
    }
  }
})

const modulesSchema = s({
github mycaule / knowledge-graph-js / services / duckduckgo.js View on Github external
/* eslint no-unused-vars: "off" */
/* eslint new-cap: "off" */
/* eslint camelcase: "off" */

const S = require('superstruct')
const isUrl = require('is-url')

const struct = S.superstruct({
  types: {
    url: isUrl,
    empty: v => v === ''
  }
})

const ResultElement = struct({
  Text: 'string',
  Icon: {
    Width: 'empty | number',
    Height: 'empty | number',
    URL: 'empty | url'
  },
  FirstURL: 'url',
  Result: 'string'
})
github ianstormtaylor / superstruct / examples / custom-types.js View on Github external
import { superstruct } from 'superstruct'
import isEmail from 'is-email'
import isUuid from 'is-uuid'
import isUrl from 'is-url'

// Define a `struct` with custom types.
const struct = superstruct({
  types: {
    uuid: v => isUuid.v4(v),
    email: v => {
      if (!isEmail(v)) return `not_email`
      if (v.length >= 256) return 'too_long'
      return true
    },
    url: v => isUrl(v) && v.length < 2048,
  },
})

// Define a struct to validate with.
const User = struct({
  id: 'uuid',
  name: 'string',
  email: 'email',
github mycaule / knowledge-graph-js / services / google.js View on Github external
/* eslint import/no-unresolved: [2, { ignore: ['\.config.js$'] }] */
/* eslint import/no-unassigned-import: "off" */
/* eslint new-cap: "off" */

const S = require('superstruct')
const isUrl = require('is-url')

const entities = ['Book', 'BookSeries', 'EducationalOrganization', 'Event', 'GovernmentOrganization', 'LocalBusiness', 'Movie', 'MovieSeries', 'MusicAlbum', 'MusicGroup', 'MusicRecording', 'Organization', 'Periodical', 'Person', 'Place', 'SportsTeam', 'TVEpisode', 'TVSeries', 'VideoGame', 'VideoGameSeries', 'WebSite']

const struct = S.superstruct({
  types: {
    url: isUrl,
    EntitySearchResult: v => v === 'EntitySearchResult',
    ItemList: v => v === 'ItemList'
  }
})

const EntitySearchResult = struct({
  '@type': 'EntitySearchResult',
  result: {
    '@id': 'string',
    name: 'string',
    '@type': ['string'],
    description: 'string',
    detailedDescription: struct.optional({
      articleBody: 'string',
github Feverqwe / ytWatchBot / src / services / youtube.js View on Github external
})
  })]
});

const ActivitiesResponse = struct.pick({
  items: [struct.pick({
    contentDetails: struct.pick({
      upload: struct.pick({
        videoId: 'string'
      })
    }),
  })],
  nextPageToken: 'string?'
});

const VideosResponse = struct.pick({
  items: [struct.pick({
    id: 'string',
    snippet: struct.pick({
      publishedAt: 'string', // 2007-03-05T08:22:25.000Z
      channelId: 'string',
      title: 'string',
      // description: 'string',
      thumbnails: struct.record(['string', struct.pick({
        url: 'string',
        width: 'number',
        height: 'number',
      })]),
      channelTitle: 'string',
      // tags: ['string'],
      // categoryId: 'string', // 10
      // liveBroadcastContent: 'string', // none
github Feverqwe / ytWatchBot / src / services / youtube.js View on Github external
id: struct.pick({
      channelId: 'string'
    })
  })]
});

const SearchItemsSnippet = struct.pick({
  items: [struct.pick({
    snippet: struct.pick({
      channelId: 'string',
      channelTitle: 'string'
    })
  })]
});

const ActivitiesResponse = struct.pick({
  items: [struct.pick({
    contentDetails: struct.pick({
      upload: struct.pick({
        videoId: 'string'
      })
    }),
  })],
  nextPageToken: 'string?'
});

const VideosResponse = struct.pick({
  items: [struct.pick({
    id: 'string',
    snippet: struct.pick({
      publishedAt: 'string', // 2007-03-05T08:22:25.000Z
      channelId: 'string',