Home

Awesome

npm version downloads build status coverage status Node.JS version

core-types-suretype

This package provides conversion functions between core-types and suretype.

You probably don't want to use this package directly, but rather typeconv which uses this package to convert between TypeScript, JSON Schema, Open API, GraphQL and suretype.

This package converts either from core-types or JSON Schema, when converting to suretype validators. It also converts either to core-types or JSON Schema when converting from suretype.

When converting to and from JSON Schema (rather than core-types), the value constraints are maintained.

It can convert from TypeScript/JavaScript files exporting suretype validators, as long as they are require()able (i.e. have all their dependencies installed).

See

Other conversion packages:

Contents

Usage

There are four conversion functions,

convertCoreTypesToSureType, convertJsonSchemaToSureType converts to suretype,

convertSureTypeToCoreTypes, convertSureTypeToJsonSchema converts from suretype.

These do all return a wrapped value, of the type ConversionResult.

core-types to suretype

import { convertCoreTypesToSureType } from 'core-types-suretype'

let doc; // This core-types document comes from somewhere

const { data: tsSourceCode } = convertCoreTypesToSureType( doc, opts );

You can provide options as a second argument of the type (it's the same type used for converting from JSON Schema, hence the name):

interface JsonSchemaToSuretypeOptions
{
	warn?: WarnFunction;
	filename?: string;
	sourceFilename?: string;
	userPackage?: string;
	userPackageUrl?: string;
	noDisableLintHeader?: boolean;
	noDescriptiveHeader?: boolean;
	useUnknown?: boolean;
	forwardSchema?: boolean;
	inlineTypes?: boolean;
	exportType?: boolean;
	exportSchema?: boolean;
	exportValidator?: boolean;
	exportEnsurer?: boolean;
	exportTypeGuard?: boolean;
	unsupported?: 'ignore' | 'warn' | 'error';
}

These options are all optional.

The warn function is of type WarnFunction from core-types, meaning it takes a message as string, and an optional second argument of type CoreTypesErrorMeta, also from core-types.

JSON Schema to suretype

Converting from JSON Schema is almost the same as from core-types;

import { convertJsonSchemaToSureType } from 'core-types-suretype'

let jsonSchema; // This JSON Schema comes from somewhere

const { data: tsSourceCode } = convertJsonSchemaToSureType( jsonSchema, opts );

The opts argument is the same as in convertCoreTypesToSureType.

suretype to core-types

import { convertSureTypeToCoreTypes } from 'core-types-suretype'

let sourceCode; // This source code comes from somewhere

const { data: doc } = await convertSureTypeToCoreTypes( sourceCode, opts );

An optional second argument can be provided of the type (this is the same type used to convert to JSON Schema, hence the name):

interface SuretypeToJsonSchemaOptions
{
	warn?: WarnFunction;
	filename?: string;
	sourceFilename?: string;
	userPackage?: string;
	userPackageUrl?: string;
	refMethod?: 'no-refs' | 'provided' | 'ref-all';
	nameConflict?: 'rename' | 'warn' | 'error';
}

suretype to JSON Schema

import { convertSureTypeToJsonSchema } from 'core-types-suretype'

let sourceCode; // This source code comes from somewhere

const { data: jsonSchema } = await convertSureTypeToJsonSchema( sourceCode, opts );

The optional opts argument is the same as in convertSureTypeToCoreTypes.