Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.4 KiB · at 74947de

Write applyPatch(profile, patch), which applies a partial update to a user profile and returns the updated profile.

A profile has a required name and optional email and phone.

Each field of the patch says what to do with the matching profile field. The signature shows how each case is written in your language:

  • keep: leave the current value (or its absence) unchanged;
  • clear (only email and phone): remove the field, so the result has no value for it. Clearing a field that is already absent is fine;
  • set: replace the value with the given string.

A set value is trimmed of surrounding whitespace, then validated, and the trimmed value is what is stored:

  • name must not be empty after trimming, else fail with { kind: "invalid_name" }.
  • email must contain exactly one @, with at least one character before it and at least one after it, else fail with { kind: "invalid_email" }.
  • phone may start with one +; after that it may contain only digits, spaces and -, and it must contain between 7 and 15 digits inclusive. Otherwise fail with { kind: "invalid_phone" }. The stored value keeps its spaces and dashes (only the outer whitespace is trimmed).

Only values being set are validated; kept values are taken as they are. If several set values are invalid, report the first in the order name, email, phone.

Return a new profile object. Do not modify profile or patch, including when the patch fails.