9.9.0 • Published 9 months ago

@apparts/types v9.9.0

Weekly downloads
57
License
MIT
Repository
github
Last release
9 months ago

#+TITLE: @apparts/types #+DATE: 2019-08-26 Mon #+AUTHOR: Philipp Uhl

This package provides functions for checking correctness of values against certain types.

  • Types

A type is defined by an object. The object must either contain a key =type= that is an atomic type or be of the form of =object=, =array=, =oneOf=, =value= as described under "Compound types".

All types definitions can be produced by the use of functions from =schema=:

#+BEGIN_SRC js import { schema } from "@apparts/types"; #+END_SRC

It is recommended, to use these functions to build type definitions, as they can also be used to infer TypeScript types.

** Atomic types:

  • ~/~ (catch all)
  • ~base64~
  • ~boolean~
  • ~email~
  • ~float~
  • ~hex~
  • ~int~
  • ~null~
  • ~phoneISD~ (phone number with country code prefix)
  • ~string~
  • ~uuidv4~

The type definition for each of the atomic types looks like: ={ type: }=.

Build a type definition for an atomic type with these functions: #+BEGIN_SRC js import { schema } from "@apparts/types"; const intSchema = schema.int(); const floatSchema = schema.float(); const booleanSchema = schema.boolean(); const stringSchema = schema.string(); const hexSchema = schema.hex(); const uuidv4Schema = schema.uuidv4(); const base64Schema = schema.base64(); const emailSchema = schema.email(); const phoneISDSchema = schema.phoneISD(); const nillSchema = schema.nill(); // { type: "null" } const anySchema = schema.any(); // { type: "/" } #+END_SRC

Certain types only differ in semantics from other more basic types. They are created like this:

#+BEGIN_SRC js // Creating ids depends on how your Ids look like, either a string or an int const idIntSchema = schema.int().semantic("id"); // { type: "int", semantic: "id" } const idStrSchema = schema.string().semantic("id"); // { type: "string", semantic: "id" }

const passwordSchema = schema.string().semantic("password"); // { type: "string", semantic: "password" } const timeSchema = schema.int().semantic("date"); // { type: "int", semantic: "date" } const timeSchema = schema.int().semantic("time"); // { type: "int", semantic: "time" } const timeSchema = schema.int().semantic("daytime"); // { type: "int", semantic: "daytime" } #+END_SRC

** Compound types

Compound objects make it possible to check complex JSON values for validity. Any sub-type can be either an atomic type or a compound type.

  • =object= :: Matches if the value is an object and all the values of the object have the types as specified by =values=, or if the specific keys of the object are known, as specified by the key in =keys=.

    • With known keys: #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const objSchema = schema.obj({ : , : , // ... });

      const typeDefinition = { type: "object", keys: { : { type: , optional: true}, ... } } // = objSchema.getType(); #+END_SRC

    • With unknown keys: #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const objSchema = schema.objValues(); const typeDefinition = { type: "object", values: } // = objSchema.getType(); #+END_SRC

  • =array= :: Matches if the value is an array and all items of the array match the type, as specified by =items=. #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const arraySchema = schema.array(); const typeDefinition = { type: "array", items: } // = arraySchema.getType(); #+END_SRC

  • =oneOf= :: Matches if at least one of the alternatives matches #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const oneOfSchema = schema.oneOf( , , // ... );

    const typeDefinition = { type: "oneOf", alternatives: , ... } // = oneOfSchema.getType(); #+END_SRC

  • =value= :: Matches the exact content #+BEGIN_SRC js // build type schema import { schema } from "@apparts/types"; const valueSchema = schema.value();

    const typeDefinition = { value: } // = valueSchema.getType(); #+END_SRC

** Using Schemas

One can build types by hand by constructing the type definition object. This is not recommended though, as it is easy to mess up and no TypeScript types can be inferred. Instead, @apparts/types provides functions to build a type definition:

#+BEGIN_SRC js // the functions then are available through schema. import { schema } from "@apparts/types"; // or directly from the package import { int, float, boolean, string, hex, uuidv4, base64, email, nill, any, array, obj, oneOf, value, InferType } from "@apparts/types"; #+END_SRC

Using a schema, one can get the type definition with the =getType= function:

#+BEGIN_SRC js const userSchema = schema.obj({ firstName: string(), lastName: string(), gender: string().optional(), }); userSchema.getType(); // returns the type definition #+END_SRC

Also, one can get a TypeScript type:

#+BEGIN_SRC ts type User = InferType;

// The resulting type looks like this: type User = { firstName: string; lastName: string; gender?: string; }; #+END_SRC

9.8.0

10 months ago

9.9.0

9 months ago

9.5.2

10 months ago

9.5.1

10 months ago

9.6.0

10 months ago

9.7.0

10 months ago

9.4.0

11 months ago

9.5.0

11 months ago

9.3.0

11 months ago

9.1.3

2 years ago

9.1.2

2 years ago

7.6.2

2 years ago

7.6.1

2 years ago

7.6.0

2 years ago

7.6.3

2 years ago

9.1.1

2 years ago

9.1.0

2 years ago

7.7.0

1 year ago

9.2.0

1 year ago

7.5.0

2 years ago

6.15.4

2 years ago

6.15.3

2 years ago

7.3.1

2 years ago

7.3.0

2 years ago

7.4.1

2 years ago

7.4.0

2 years ago

7.2.1

2 years ago

8.0.0

2 years ago

9.0.0

2 years ago

6.15.2

2 years ago

7.1.0

2 years ago

7.0.5

2 years ago

6.15.1

2 years ago

6.15.0

2 years ago

7.0.0

2 years ago

7.0.4

2 years ago

7.0.3

2 years ago

7.2.0

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

6.13.2

2 years ago

6.14.2

2 years ago

6.14.1

2 years ago

6.14.0

2 years ago

6.13.1

2 years ago

6.11.3

2 years ago

6.13.0

2 years ago

6.12.0

2 years ago

6.11.1

2 years ago

6.11.0

2 years ago

6.11.2

2 years ago

6.10.2

2 years ago

6.10.1

2 years ago

6.10.0

2 years ago

6.10.4

2 years ago

6.10.3

2 years ago

6.9.3

2 years ago

6.9.2

2 years ago

6.8.1

3 years ago

6.8.2

3 years ago

6.7.0

3 years ago

6.8.0

3 years ago

6.6.3

3 years ago

6.6.1

3 years ago

6.6.2

3 years ago

6.6.0

3 years ago

6.5.0

3 years ago

6.4.1

3 years ago

6.4.0

3 years ago

6.3.0

3 years ago

6.2.0

3 years ago

6.1.2

3 years ago

6.1.1

3 years ago

6.1.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.1.0

3 years ago

4.0.1

3 years ago

4.0.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.6.1

4 years ago

2.6.0

4 years ago

2.5.2

4 years ago

2.5.1

4 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.4

4 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago