5.8.8 • Published 9 months ago

@powership/schema v5.8.8

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

@powership/schema

Typescript schema validation with static type inference.

Schemas are a crucial part of a microservices architecture or a detachable application architecture (global.powership). They can serve as contracts between different pieces of an application (frontend, backend, forms) and different services. So schemas should be easily portable, written and read, and that's the goal of this package.

image

Installation

To install:

npm install @powership/schema

⚠️ IMPORTANT: You must enable strict mode in your tsconfig.json. This is a best practice for all TypeScript projects.

// tsconfig.json
{
  // ...
  "compilerOptions": {
    // ...
    "strict": true
  }
}

Usage

Schema

  const addressSchema = createSchema({
    street: 'string',
    number: 'int?',
  });

  const schemaDefinition = {
    name: 'string', // any string
    email: 'email?', // email type - will validate against email regex
    age: 'int?', // optional integer
    notes: '[int]?',

    // declaring an union
    unionField: [['string?', '[int]?']],

    // represents an enum
    letter: ['a', 'b', 'c'],

    // more detailed way to define enums
    letterOptionalList: {
      enum: ['x', 'y', 'z'],
      optional: true,
      list: true,
    },

    // using a previous schema as field type
    optionalAddress: {
      type: addressSchema,
      optional: true,
    },

    // another way to define schema fields
    deliveryAddress: {
      schema: {
        street: 'string',
        number: 'int?',
      },
    },
  } as const; // "as const" is needed to TS to infer types correctly

  const userSchema = createSchema(schemaDefinition);

  expect(() => userSchema.parse({ name: 'Antonio', letter: 'x' })).toThrow(
    `field "letter": accepted: 'a' or 'b' or 'c', found x.`
  );

  expect(() => userSchema.parse({ name: 'antonio', letter: 'a', deliveryAddress: {} })).toThrow(
    'field "deliveryAddress": ➤ field "street": expected type string, found undefined.'
  );

  const parsed = userSchema.parse({ name: 'antonio', letter: 'a', deliveryAddress: { street: 'alameda' } });

  type InferType = typeof parsed;

  assert<
    IsExact<
      InferType,
      {
        name: string;
        email?: string | undefined;
        age?: number | undefined;
        notes?: number[] | undefined;
        unionField?: string | number[] | undefined;
        letter: 'a' | 'b' | 'c';
        letterOptionalList?: ('x' | 'y' | 'z')[] | undefined;
        optionalAddress?:
          | {
              street: string;
              number?: number | undefined;
            }
          | undefined;

        deliveryAddress: {
          street: string;
          number?: number | undefined;
        };
      }
    >
  >(true);

schemaToTypescript

Returns a string of an interface representing a PowershipSchema;

import { schemaToTypescript } from '@powership/schema/lib/schemaToTypescript';

const interfaceTxt = await schemaToTypescript('User', userSchema);

const interfaceTxt = await schemaToTypescript('User', userSchema);
expect(interfaceTxt).toBe(`
export interface User {
  name: string;
  email?: Email;
  age?: number;
  notes?: number[];
  unionField?: string | number[];
  letter: "a" | "b" | "c";
  letterOptionalList?: ("x" | "y" | "z")[];
  optionalAddress?: {
    street: string;
    number?: number;
  };
  deliveryAddress: {
    street: string;
    number?: number;
  };
}`);

schemaToJSON

Receives a PowershipSchema and returns a json-schema

  import { schemaToJSON } from '@powership/schema/lib/schemaToJSON';

  const jsonSchema = schemaToJSON('User', userSchema);

  expect(jsonSchema).toEqual({
    properties: {
      address: {
        properties: {
          number: {
            type: 'integer',
          },
          street: {
            type: 'string',
          },
        },
        required: ['street'],
        title: '',
        type: 'object',
      },
      age: {
        type: 'integer',
      },
      email: {
        tsType: 'Email',
        type: 'string',
      },
      letter: {
        enum: ['a', 'b', 'c'],
        title: 'EnumLetterUser',
        type: 'string',
      },
      letterOptionalList: {
        items: {
          enum: ['x', 'y', 'z'],
          title: 'Enum__subLetterOptionalList',
          type: 'string',
        },
        type: 'array',
      },
      name: {
        type: 'string',
      },
      notes: {
        items: {
          type: 'integer',
        },
        type: 'array',
      },
    },
    required: ['name', 'letter'],
    title: 'User',
    type: 'object',
  });

TODO

  • improve documentation
5.8.8

9 months ago

5.8.7

9 months ago

5.8.6

9 months ago

5.8.5

9 months ago

5.8.4

9 months ago

5.8.3

9 months ago

5.8.2

9 months ago

5.8.1

9 months ago

5.8.0

11 months ago

5.5.5

11 months ago

5.1.6

11 months ago

5.1.3

11 months ago

5.1.2

11 months ago

5.1.1

11 months ago

5.1.0

12 months ago

5.0.807

12 months ago

5.0.808

12 months ago

5.0.809

12 months ago

5.0.803

12 months ago

5.0.804

12 months ago

5.0.806

12 months ago

5.0.801

12 months ago

5.0.802

12 months ago

5.7.10

11 months ago

5.0.814

12 months ago

5.0.815

12 months ago

5.0.816

12 months ago

5.0.817

12 months ago

5.7.12

11 months ago

5.0.810

12 months ago

5.7.11

11 months ago

5.6.3

11 months ago

5.0.811

12 months ago

5.7.14

11 months ago

5.6.2

11 months ago

5.0.812

12 months ago

5.7.13

11 months ago

5.6.1

11 months ago

5.6.0

11 months ago

5.7.9

11 months ago

5.7.7

11 months ago

5.1.11

11 months ago

5.1.10

11 months ago

5.0.610

1 year ago

4.1.35

1 year ago

5.0.25

1 year ago

4.0.5

1 year ago

4.0.4

1 year ago

4.0.7

1 year ago

4.0.6

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.0.3

1 year ago

4.0.2

1 year ago

4.0.9

1 year ago

4.0.8

1 year ago

4.1.350

1 year ago

5.0.800

1 year ago

4.0.10

1 year ago

4.0.11

1 year ago

5.0.700

1 year ago

4.1.0

1 year ago

4.1.1

1 year ago

3.3.14

2 years ago

3.3.15

2 years ago

3.3.16

2 years ago

3.3.17

2 years ago

3.3.18

2 years ago

3.3.19

2 years ago

3.3.20

2 years ago

3.3.21

2 years ago

3.3.22

2 years ago

3.3.23

2 years ago

3.3.13

2 years ago

3.3.10

2 years ago

3.3.11

2 years ago

3.3.12

2 years ago

3.3.9

2 years ago

3.3.8

2 years ago

3.3.7

2 years ago

3.3.5

2 years ago

3.3.4

2 years ago

3.3.3

2 years ago

3.3.1

2 years ago

3.3.2

2 years ago

3.3.1-beta.1

2 years ago

3.3.1-beta.2

2 years ago

3.3.1-beta.3

2 years ago

3.2.27

2 years ago

3.2.25-beta.2

2 years ago

3.2.25-beta.1

2 years ago

3.2.25-beta.5

2 years ago

3.2.25-beta.4

2 years ago

3.2.25-beta.3

2 years ago

3.3.0

2 years ago

3.2.24

2 years ago

3.2.23

2 years ago

3.2.20

2 years ago

3.2.22

2 years ago

3.2.21

2 years ago

3.2.16

2 years ago

3.2.12

2 years ago

3.2.15

2 years ago

3.2.11

2 years ago

3.2.10

2 years ago

3.2.8-beta.0

2 years ago

3.2.6

2 years ago

3.2.9

2 years ago

3.2.7

2 years ago

3.2.8-beta.3

2 years ago

3.2.8-beta.2

2 years ago

3.2.8-beta.4

2 years ago

3.2.5

2 years ago

3.2.6-beta.10

2 years ago

3.2.6-beta.11

2 years ago

3.2.6-beta.6

2 years ago

3.2.6-beta.5

2 years ago

3.2.6-beta.4

2 years ago

3.2.6-beta.3

2 years ago

3.2.1-beta.4

2 years ago

3.2.6-beta.2

2 years ago

3.2.1-beta.3

2 years ago

3.2.6-beta.1

2 years ago

3.2.6-beta.0

2 years ago

3.2.5-beta.1

2 years ago

3.2.5-beta.2

2 years ago

3.2.1-beta.1

2 years ago

3.2.5-beta.3

2 years ago

3.2.5-beta.4

2 years ago

3.2.6-beta.9

2 years ago

3.2.6-beta.8

2 years ago

3.1.12-beta.10

2 years ago

3.1.12-beta.12

2 years ago

3.1.12-beta.8

2 years ago

3.1.12-beta.9

2 years ago

3.2.0

2 years ago

3.1.12-beta.5

2 years ago

3.1.12-beta.0

2 years ago

3.1.12-beta.1

2 years ago

3.1.12-beta.2

2 years ago

3.1.12-beta.3

2 years ago

3.1.10

2 years ago

2.0.5-beta.2

2 years ago

2.3.1-beta.0

2 years ago

2.3.0

2 years ago

2.3.1-beta.2

2 years ago

2.0.5

2 years ago

2.3.1

2 years ago

2.3.1-beta.1

2 years ago

2.0.4

2 years ago

2.0.5-pre.4

2 years ago

2.0.5-pre.3

2 years ago

2.0.5-pre.2

2 years ago

2.0.5-pre.1

2 years ago

2.0.5-pre.8

2 years ago

2.0.5-pre.7

2 years ago

3.1.9

2 years ago

2.0.5-pre.6

2 years ago

3.1.8

2 years ago

2.0.5-pre.5

2 years ago

2.3.1-beta.3

2 years ago

2.0.5-pre.9

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago