warengonzaga / gathertown.js

Simple and lightweight community contributed unofficial JS/TS SDK for Gather Town's HTTPS and WebSocket APIs. 🌏🕹💬
https://gathertown.js.org
MIT License
40 stars 6 forks source link

add yup for prop schema validation #42

Closed princejoogie closed 2 years ago

princejoogie commented 2 years ago

this PR resolves #21

How it works

create a schema for each request:

const createSpaceSchema = yup.object({
  apiKey: yup.string().required().trim(),
  name: yup.string().required().trim(),
  map: yup.string().trim(),
  reason: yup.string().trim(),
  sourceSpace: yup.string().trim(),
});

then validate the passed props in each request:

export const handleCreateSpace = async (props: CreateSpaceProps) => {
  try {
    const { apiKey, map, name, reason, sourceSpace } = await createSpaceSchema.validate(props);
    // here we handle if everything else is okay
  } catch (err) {
    // here we throw the validation errors as well as the errors from gathertowns' endpoint
    const error: any = err;
    throw new Error(error.message);
  }
warengonzaga commented 2 years ago

Awesome, @princejoogie! LGTM 🔥