How to use the superstruct.struct function in superstruct

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 cnguy / kayn / lib / KaynConfig.js View on Github external
},
}

const ttlsValidator = {}
Object.keys(METHOD_NAMES).forEach(s => {
    Object.keys(METHOD_NAMES[s]).forEach(
        t => (ttlsValidator[METHOD_NAMES[s][t]] = 'number?'),
    )
})

const byGroupValidator = {}
Object.keys(METHOD_NAMES).forEach(groupName => {
    byGroupValidator[groupName] = 'number?'
})

export const KAYN_CONFIG_STRUCT = struct({
    region: 'string',
    locale: 'string',
    apiURLPrefix: 'string',
    debugOptions: {
        isEnabled: 'boolean',
        showKey: 'boolean',
        loggers: 'any',
    },
    requestOptions: {
        shouldRetry: 'boolean',
        numberOfRetriesBeforeAbort: 'number',
        delayBeforeRetry: 'number',
        burst: 'boolean',
        shouldExitOn403: 'boolean',
    },
    cacheOptions: {
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
const gatewayStructure = struct({
    name: 'string',
    url: 'string',
    serverOptions: serverOptionsStructure,
    fragments: [gatewayFragmentStructure],
    api: [apiStructure],
    isMobile: 'boolean?',
    authToken: 'string?',
    fragmentsFolder: 'string',
    corsDomains: struct.optional(['string']),
    corsMaxAge: 'number?',
    customHeaders: struct.optional([customHeaderStructure])
});

const storefrontPageStructure = struct({
    page: 'string?',
    html: 'string',
    url: struct.union(['string', ['string']]),
    condition: 'function?'
});

const storefrontGatewaysStructure = struct({
    name: 'string',
    url: 'string',
    assetUrl: 'string?'
});

const storefrontDependencyStructure = struct({
    content: 'string?',
    link: 'string?'
});
github vuejs / vue-component-compiler / src / assemble.js View on Github external
function inlineStyle (name, style, config) {
  let output = `var ${name} = ${style.modules ? _s(style.modules) : '{}'}\n`

  output +=
    `${name}.__inject__ = function (context) {\n` +
    `  ${STYLE_INJECTOR_IDENTIFIER}(${_s(config.shortFilePath)}, [[${_s(
      config.shortFilePath
    )}, ${_s(style.code)}, ${_s(style.descriptor.attrs.media)}, ${_s(
      style.map
    )}]], ${config.isProduction}, context)\n` +
    `}\n`

  return output
}

const Source = struct({
  script: {
    id: struct.union(['string?', 'null']),
    code: struct.union(['string?', 'null']),
    map: 'object?',
    descriptor: struct.union(['object', 'null'])
  },
  render: {
    id: struct.union(['string?', 'null']),
    code: struct.union(['string?', 'null']),
    map: 'object?',
    descriptor: struct.union(['object', 'null'])
  },
  styles: struct.list([
    {
      id: struct.union(['string?', 'null']),
      code: struct.union(['string?', 'null']),
github saberland / saber / packages / saber / src / utils / validateConfig.ts View on Github external
const locales = struct('object', {})

  const theme = struct('string?')

  const template = struct(
    {
      openLinkInNewTab: 'boolean',
      plugins: ['any']
    },
    {
      openLinkInNewTab: true,
      plugins: []
    }
  )

  const schema = struct({
    build,
    siteConfig,
    themeConfig,
    locales,
    theme,
    plugins,
    markdown,
    permalinks,
    server,
    template
  })

  const [err, result] = schema.validate(config)

  if (err) {
    throw new Error(`Invalid Saber config: ${err.message}`)
github saberland / saber / packages / saber / src / utils / validateConfig.ts View on Github external
lazy: 'boolean?',
      outDir: 'string?',
      cache: 'boolean?'
    },
    {
      publicUrl: '/',
      extractCSS: false,
      loaderOptions: {},
      cssSourceMap: false,
      lazy: false,
      outDir: 'public',
      cache: true
    }
  )

  const locales = struct('object', {})

  const theme = struct('string?')

  const template = struct(
    {
      openLinkInNewTab: 'boolean',
      plugins: ['any']
    },
    {
      openLinkInNewTab: true,
      plugins: []
    }
  )

  const schema = struct({
    build,
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
fileName: 'string',
    link: 'string?',
    loadMethod: struct.enum(Object.values(RESOURCE_LOADING_TYPE)),
    type: struct.enum(Object.values(RESOURCE_TYPE)),
    dependent: struct.optional(['string'])
});

const gatewayFragmentDependenctStructure = struct({
    name: 'string',
    type: struct.enum(Object.values(RESOURCE_TYPE)),
    link: 'string?',
    preview: 'string?',
    injectType: struct.optional(struct.enum(Object.values(RESOURCE_INJECT_TYPE)))
});

const gatewayFragmentVersionStructure = struct({
    assets: [gatewayFragmentAssetsStructure],
    dependencies: [gatewayFragmentDependenctStructure],
    handler: 'string?'
});


const gatewayFragmentStructure = struct({
    name: 'string',
    testCookie: 'string',
    prg: struct.optional('boolean'),
    render: gatewayRenderStructure,
    warden: struct.optional('object'),
    version: 'string',
    versionMatcher: 'string?',
    versions: struct.dict(['string', gatewayFragmentVersionStructure])
});