Schema

JSON

function parseJSON(source, message) {
  if (typeof message === "function") {
    const gen = message();
    let reply;
    while (true) {
      const result = gen.next(reply != null ? reply : "");
      if (result.done) {
        return result.value;
      }
      const value = result.value;
      reply = undefined;
      if (source instanceof Object && "name" in value && typeof value.name === "string" && value.name in source) {
        reply = parseJSON(source[value.name], value);
        if (typeof value === "function") {
        } else if (value.type === types.array) {
          const itemType = value.itemType;
          if (Array.isArray(source[value.name])) {
            const sanitizedItems = source[value.name].map((x) => parseJSON(x, itemType));
            reply = sanitizedItems;
          }
        }
      }
    }
  } else if (message.type === types.string) {
    if (typeof source === "string") {
      return source;
    }
  } else if (message.type === types.number) {
    if (typeof source === "number") {
      return source;
    }
  } else if (message.type === types.boolean) {
    if (typeof source === "boolean") {
      return source;
    }
  } else if (message.type === types.array) {
    if (Array.isArray(source)) {
      return source.map((item) => parseJSON(item, message.itemType));
    }
  }
  return null;
}

Form Data

function parseFormData(source, generator) {
  const gen = generator();
  let reply;
  while (true) {
    const result = gen.next(reply != null ? reply : "");
    if (result.done) {
      return result.value;
    }
    reply = undefined;
    if (result.value.type === types.string) {
      if (source.has(result.value.name)) {
        reply = source.get(result.value.name);
      }
    } else if (result.value.type === types.number) {
      if (source.has(result.value.name)) {
        reply = parseFloat(source.get(result.value.name));
      }
    }
  }
}

AWS Region Schema

function* AWSRegionSchema() {
  const primary = yield read.string("primary");
  const secondary = yield read.string("secondary");
  const digit = yield read.number("digit");
  return {
    primary,
    secondary,
    digit
  };
}

Results

{
  "primary": "us",
  "secondary": "east",
  "digit": 1
}

Profile Form

function* ProfileSchema() {
  const bio = yield read.string("bio");
  const favoriteNumber = yield read.number("favoriteNumber");
  return {
    bio,
    favoriteNumber
  };
}

Interactive Preview

{
  "bio": "",
  "favoriteNumber": 7
}

Fetched Data

Results

null

Resource Data

Results

[
  null,
  {
    "startCount": 0,
    "completedCount": 0,
    "data": null
  }
]