Keep what you cannot interpret
A read is lenient and a write is conservative, which together give a useful property: a document you do not fully understand still round-trips. An element the reader did not expect is preserved and reported rather than dropped:
import { parseResource, serializeResource, validateResource } from "@cosyte/fhir";
const source =
'{"resourceType":"Observation","status":"final","code":{"text":"synthetic"},"vendorField":"kept"}';
const { resource } = parseResource(source);
validateResource(resource).issues.map((issue) => issue.code); // => ["UNKNOWN_ELEMENT"]
serializeResource(resource) === source; // => true
UNKNOWN_ELEMENT is a warning on a lenient read and an error in strict mode, which is the mode to
use when you are the one emitting a document rather than the one receiving it.
If the write cannot be done without inventing or losing content, it is refused with a coded reason instead. Troubleshooting lists the refusals and what each one means.
Reference
parseResourceandserializeResource.validateResourceand itsmodeoption inValidateOptions.SERIALIZE_ERROR_CODES, every reason a write is refused.