In the previous chapters, we validated the body, URL-encoded params, and headers independently. But sometimes the response’s meaning depends on all three together. The status code determines the body shape, or you need typed values from both headers and the body.
HttpClientResponse.schemaJson() validates the response body, status code, and headers as a single structure. You define one schema that describes all three, and the validation happens in one step.
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse,} from "effect/unstable/http";import { Effect, Schema } from "effect";
// Define a schema that validates status, headers, and body togetherconst UserResponse = Schema.Struct({ status: Schema.Literal(200), headers: Schema.Struct({ "content-type": Schema.String, }), body: Schema.Struct({ id: Schema.Number, firstName: Schema.String, lastName: Schema.String, email: Schema.String, }),});
function fetchUser(userId: number) { return Effect.gen(function* () { const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.get( `https://dummyjson.com/users/${userId}`, );
const response = yield* client.execute(request);
// Validate status + headers + body in one step const result = yield* HttpClientResponse.schemaJson(UserResponse)(response);
return result; }).pipe(Effect.provide(FetchHttpClient.layer));}
// Test itEffect.runPromise(fetchUser(1)).then((result) => { console.log("Status:", result.status); console.log("Content-Type:", result.headers["content-type"]); console.log("User:", result.body.firstName, result.body.lastName); console.log("Email:", result.body.email);});Output:
Status: 200Content-Type: application/json; charset=utf-8User: Emily JohnsonEmail: emily.johnson@x.dummyjson.comschemaJson is a curried function, just like schemaBodyJson. You pass the schema first, then the response:
HttpClientResponse.schemaJson(UserResponse)(response);// ^^^^^^^^^^^^ ^^^^^^^^// schema responseUnder the hood, schemaJson calls response.json to parse the body, then constructs an object with { status, headers, body } and runs the schema validation against it. If any part doesn’t match — wrong status code, missing header, or unexpected body shape, you get a SchemaError.
The key difference from schemaBodyJson is scope. schemaBodyJson validates only the body. schemaJson validates the full response context.