Skip to content

Instantly share code, notes, and snippets.

@ehaynes99
Created March 21, 2023 00:04
Show Gist options
  • Save ehaynes99/043ad96710f7b0c7837af7182c88e2b1 to your computer and use it in GitHub Desktop.
Save ehaynes99/043ad96710f7b0c7837af7182c88e2b1 to your computer and use it in GitHub Desktop.
TypeBox example

Schema Definition

export const Customer = Type.Object({
  firstName: Type.String(),
  lastName: Type.String(),
  age: Type.Number(),
  dob: Type.Date(),
  address: Type.Object({
    streetAddress: Type.String(),
    city: Type.String(),
    state: Type.String(),
    postalCode: Type.String(),
  }),
  contact: Type.Array(
    Type.Object({
      type: Type.String({ default: 'Joe' }),
      value: Type.String(),
    }),
  ),
})

Type Inference

type Customer = {
    firstName: string;
    lastName: string;
    age: number;
    dob: Date;
    address: {
        streetAddress: string;
        city: string;
        state: string;
        postalCode: string;
    };
    contact: {
        type: string;
        value: string;
    }[];
}

JSON Schema object

{
  type: 'object',
  properties: {
    firstName: {
      type: 'string',
    },
    lastName: {
      type: 'string',
    },
    age: {
      type: 'number',
    },
    dob: {
      type: 'object',
      instanceOf: 'Date',
    },
    address: {
      type: 'object',
      properties: {
        streetAddress: {
          type: 'string',
        },
        city: {
          type: 'string',
        },
        state: {
          type: 'string',
        },
        postalCode: {
          type: 'string',
        },
      },
      required: ['streetAddress', 'city', 'state', 'postalCode'],
    },
    contact: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          type: {
            default: 'Joe',
            type: 'string',
          },
          value: {
            type: 'string',
          },
        },
        required: ['type', 'value'],
      },
    },
  },
  required: ['firstName', 'lastName', 'age', 'dob', 'address', 'contact'],
}

Compiled Validator (highly performant)

return function check(value) {
  return (
    (typeof value === 'object' && value !== null && !Array.isArray(value)) &&
    (typeof value.firstName === 'string') &&
    (typeof value.lastName === 'string') &&
    (typeof value.age === 'number' && !isNaN(value.age)) &&
    (value.dob instanceof Date) && !isNaN(value.dob.getTime()) &&
    (typeof value.address === 'object' && value.address !== null && !Array.isArray(value.address)) &&
    (typeof value.address.streetAddress === 'string') &&
    (typeof value.address.city === 'string') &&
    (typeof value.address.state === 'string') &&
    (typeof value.address.postalCode === 'string') &&
    (Array.isArray(value.contact) && value.contact.every(value => ((typeof value === 'object' && value !== null && !Array.isArray(value)) && (typeof value.type === 'string') && (typeof value.value === 'string'))))
 )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment