How to use the superstruct.struct.optional 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 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 puzzle-js / puzzle-js / src / configurator.ts View on Github external
versionMatcher: 'string?',
    versions: struct.dict(['string', gatewayFragmentVersionStructure])
});

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({
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
import {sealed} from "./decorators";
import {struct} from "superstruct";
import {IGatewayBFFConfiguration, IStorefrontConfig} from "./types";
import {ERROR_CODES, PuzzleError} from "./errors";
import {HTTP_METHODS, INJECTABLE, RESOURCE_INJECT_TYPE} from "./enums";
import {RESOURCE_LOADING_TYPE, RESOURCE_TYPE} from "@puzzle-js/client-lib/dist/enums";

const httpsStructure = struct({
    allowHTTP1: struct.optional('boolean'),
    key: struct.optional(struct.union(['string', 'buffer'])),
    cert: struct.optional(struct.union(['string', 'buffer']))
});

const serverOptionsStructure = struct({
    port: struct.optional('number'),
    hostname: struct.optional('string'),
    http2: struct.optional('boolean'),
    https: struct.optional(httpsStructure)
});

const apiEndpointsStructure = struct({
    path: 'string',
    middlewares: struct.optional(['string']),
    method: struct.enum(Object.values(HTTP_METHODS)),
    controller: 'string',
    routeCache: 'number?',
    cacheControl: 'string?'
});

const customHeaderStructure = struct({
    key: 'string',
    value: struct.union(['string', 'number']),
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
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])
});

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?',
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
import {sealed} from "./decorators";
import {struct} from "superstruct";
import {IGatewayBFFConfiguration, IStorefrontConfig} from "./types";
import {ERROR_CODES, PuzzleError} from "./errors";
import {HTTP_METHODS, INJECTABLE, RESOURCE_INJECT_TYPE} from "./enums";
import {RESOURCE_LOADING_TYPE, RESOURCE_TYPE} from "@puzzle-js/client-lib/dist/enums";

const httpsStructure = struct({
    allowHTTP1: struct.optional('boolean'),
    key: struct.optional(struct.union(['string', 'buffer'])),
    cert: struct.optional(struct.union(['string', 'buffer']))
});

const serverOptionsStructure = struct({
    port: struct.optional('number'),
    hostname: struct.optional('string'),
    http2: struct.optional('boolean'),
    https: struct.optional(httpsStructure)
});

const apiEndpointsStructure = struct({
    path: 'string',
    middlewares: struct.optional(['string']),
    method: struct.enum(Object.values(HTTP_METHODS)),
    controller: 'string',
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
warden: struct.optional('object'),
    version: 'string',
    versionMatcher: 'string?',
    versions: struct.dict(['string', gatewayFragmentVersionStructure])
});

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?'
});
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
import {sealed} from "./decorators";
import {struct} from "superstruct";
import {IGatewayBFFConfiguration, IStorefrontConfig} from "./types";
import {ERROR_CODES, PuzzleError} from "./errors";
import {HTTP_METHODS, INJECTABLE, RESOURCE_INJECT_TYPE} from "./enums";
import {RESOURCE_LOADING_TYPE, RESOURCE_TYPE} from "@puzzle-js/client-lib/dist/enums";

const httpsStructure = struct({
    allowHTTP1: struct.optional('boolean'),
    key: struct.optional(struct.union(['string', 'buffer'])),
    cert: struct.optional(struct.union(['string', 'buffer']))
});

const serverOptionsStructure = struct({
    port: struct.optional('number'),
    hostname: struct.optional('string'),
    http2: struct.optional('boolean'),
    https: struct.optional(httpsStructure)
});

const apiEndpointsStructure = struct({
    path: 'string',
    middlewares: struct.optional(['string']),
    method: struct.enum(Object.values(HTTP_METHODS)),
    controller: 'string',
    routeCache: 'number?',
    cacheControl: 'string?'
});

const customHeaderStructure = struct({
    key: 'string',
github puzzle-js / puzzle-js / src / configurator.ts View on Github external
const apiStructure = struct({
    name: 'string',
    testCookie: 'string',
    liveVersion: 'string',
    versions: struct.dict(['string', apiVersionStructure])
});

const gatewayRenderStructure = struct({
    url: struct.union(['string', ['string']]),
    static: 'boolean?',
    selfReplace: 'boolean?',
    placeholder: 'boolean?',
    error: 'boolean?',
    timeout: 'number?',
    middlewares: struct.optional(['string']),
    routeCache: 'number?'
});

const gatewayFragmentAssetsStructure = struct({
    name: 'string',
    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?',