How to use optimal - 10 common examples

To help you get started, we’ve selected a few optimal 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 milesj / boost / packages / core / src / Tool.ts View on Github external
{
        appName: string()
          .required()
          .notEmpty()
          .match(APP_NAME_PATTERN),
        appPath: string()
          .required()
          .notEmpty(),
        argOptions: object(),
        configBlueprint: object(),
        configName: string().custom(value => {
          if (value && !value.match(CONFIG_NAME_PATTERN)) {
            throw new Error('Config file name must be camel case without extension.');
          }
        }),
        footer: string(),
        header: string(),
        root: string(process.cwd()),
        scoped: bool(),
        settingsBlueprint: object(),
        workspaceRoot: string(),
      },
      {
        name: this.constructor.name,
      },
    );

    this.appPath = Path.resolve(this.options.appPath);
    this.rootPath = Path.resolve(this.options.root);

    // Set environment variables
    env('DEBUG_GLOBAL_NAMESPACE', this.options.appName);
github milesj / boost / packages / core / src / Tool.ts View on Github external
.required()
          .notEmpty()
          .match(APP_NAME_PATTERN),
        appPath: string()
          .required()
          .notEmpty(),
        argOptions: object(),
        configBlueprint: object(),
        configName: string().custom(value => {
          if (value && !value.match(CONFIG_NAME_PATTERN)) {
            throw new Error('Config file name must be camel case without extension.');
          }
        }),
        footer: string(),
        header: string(),
        root: string(process.cwd()),
        scoped: bool(),
        settingsBlueprint: object(),
        workspaceRoot: string(),
      },
      {
        name: this.constructor.name,
      },
    );

    this.appPath = Path.resolve(this.options.appPath);
    this.rootPath = Path.resolve(this.options.root);

    // Set environment variables
    env('DEBUG_GLOBAL_NAMESPACE', this.options.appName);

    // Core debugger, logger, and translator for the entire tool
github milesj / boost / packages / core / src / ConfigLoader.ts View on Github external
// prettier-ignore
      pluginsBlueprint[pluralName] = array(union>([
        string().notEmpty(),
        shape({ [singularName]: string().notEmpty() }),
        instance(contract, true),
      ], []));
    });

    const config = optimal(
      this.inheritFromArgs(this.parseAndExtend(configPath), args),
      {
        ...configBlueprint,
        ...pluginsBlueprint,
        debug: bool(),
        extends: array(string()),
        locale: string(),
        output: number(2).between(1, 3, true),
        // shape() requires a non-empty object
        settings: isEmpty(settingsBlueprint) ? object() : shape(settingsBlueprint),
        silent: bool(),
        theme: string('default').notEmpty(),
      },
      {
        file: configPath instanceof Path ? configPath.name() : '',
        name: 'ConfigLoader',
        unknown: true,
      },
    );

    return (config as unknown) as T;
  }
github milesj / boost / packages / core / src / ConfigLoader.ts View on Github external
], []));
    });

    const config = optimal(
      this.inheritFromArgs(this.parseAndExtend(configPath), args),
      {
        ...configBlueprint,
        ...pluginsBlueprint,
        debug: bool(),
        extends: array(string()),
        locale: string(),
        output: number(2).between(1, 3, true),
        // shape() requires a non-empty object
        settings: isEmptyObject(settingsBlueprint) ? object() : shape(settingsBlueprint),
        silent: bool(),
        theme: string('default').notEmpty(),
      },
      {
        file: typeof configPath === 'string' ? path.basename(configPath) : '',
        name: 'ConfigLoader',
        unknown: true,
      },
    );

    return config as T;
  }
github milesj / shapeshifter / packages / core / src / definitions / Enum.ts View on Github external
constructor(options: Options, attribute: string, config: Partial = {}) {
    super(options, attribute, config, false);

    this.config = optimal(
      this.config,
      {
        constant: bool(),
        nullable: bool(),
        optional: bool(),
        type: string('enum').notEmpty(),
        valueType: this.createUnionType(),
        // `valueType` must be validated before values
        values: array(custom(this.validateValue, ''))
          .notEmpty()
          .required(),
      },
      {
        name: 'EnumDefinition',
        unknown: true,
      },
    );
  }
github milesj / boost / packages / core / src / ConfigLoader.ts View on Github external
this.debug('Generating %s blueprint', chalk.magenta(singularName));

      // prettier-ignore
      pluginsBlueprint[pluralName] = array(union>([
        string().notEmpty(),
        shape({ [singularName]: string().notEmpty() }),
        instance(contract as Constructor, true),
      ], []));
    });

    const config = optimal(
      this.inheritFromArgs(this.parseAndExtend(configPath), args),
      {
        ...configBlueprint,
        ...pluginsBlueprint,
        debug: bool(),
        extends: array(string()),
        locale: string(),
        output: number(2).between(1, 3, true),
        // shape() requires a non-empty object
        settings: isEmptyObject(settingsBlueprint) ? object() : shape(settingsBlueprint),
        silent: bool(),
        theme: string('default').notEmpty(),
      },
      {
        file: typeof configPath === 'string' ? path.basename(configPath) : '',
        name: 'ConfigLoader',
        unknown: true,
      },
    );

    return config as T;
github milesj / boost / packages / core / src / ConfigLoader.ts View on Github external
instance(contract as Constructor, true),
      ], []));
    });

    const config = optimal(
      this.inheritFromArgs(this.parseAndExtend(configPath), args),
      {
        ...configBlueprint,
        ...pluginsBlueprint,
        debug: bool(),
        extends: array(string()),
        locale: string(),
        output: number(2).between(1, 3, true),
        // shape() requires a non-empty object
        settings: isEmptyObject(settingsBlueprint) ? object() : shape(settingsBlueprint),
        silent: bool(),
        theme: string('default').notEmpty(),
      },
      {
        file: typeof configPath === 'string' ? path.basename(configPath) : '',
        name: 'ConfigLoader',
        unknown: true,
      },
    );

    return config as T;
  }
github milesj / boost / packages / core / src / outputs / ProgressOutput.ts View on Github external
protected toString(state: ProgressState): string {
    const { color, current, style: styleName, template, total, transparent } = optimal(
      state,
      {
        color: bool(),
        current: number()
          .required()
          .gte(0),
        style: string('bar').oneOf(Object.keys(STYLES) as ProgressStyle[]),
        template: string('{percent} {bar} {progress}').notEmpty(),
        total: number()
          .required()
          .gt(0),
        transparent: bool(),
      },
      {
        name: 'ProgressOutput',
      },
    );

    // Mark as final for convenience
github milesj / boost / src / ConsoleOLD.js View on Github external
constructor(reporter: Tr, options: Partial = {}) {
    super();

    this.reporter = reporter;
    this.options = optimal(options, {
      footer: string().empty(),
      header: string().empty(),
      silent: bool(),
    });

    // Avoid binding listeners while testing
    if (process.env.NODE_ENV === 'test') {
      return;
    }

    // Start early so we may capture uncaught/unhandled
    this.start();
  }
github milesj / shapeshifter / packages / core / src / definitions / Enum.ts View on Github external
constructor(options: Options, attribute: string, config: Partial = {}) {
    super(options, attribute, config, false);

    this.config = optimal(
      this.config,
      {
        constant: bool(),
        nullable: bool(),
        optional: bool(),
        type: string('enum').notEmpty(),
        valueType: this.createUnionType(),
        // `valueType` must be validated before values
        values: array(custom(this.validateValue, ''))
          .notEmpty()
          .required(),
      },
      {
        name: 'EnumDefinition',
        unknown: true,
      },

optimal

Build, validate, and transform values with immutable typed schemas.

MIT
Latest version published 3 years ago

Package Health Score

47 / 100
Full package analysis