GillianPerard/typescript-json-serializer

[BUG]: JsonSerializer.serialize doesn't respect JsonProperty({ required: true })

shcallaway opened this issue · 4 comments

Version

4.0.1

Description

JsonSerializer is not throwing an error when required properties are missing, even after being configured with errorCallback: throwError.

NOTE THAT THE VERSION ABOVE (4.0.1) IS WRONG. I AM USING 4.3.0. FOR SOME REASON, THIS WAS NOT AN OPTION IN DROPDOWN.

Error

No response

Reproduction

This code does not throw an error when it definitely should:

import {
  JsonObject,
  JsonProperty,
  JsonSerializer,
  throwError,
} from "typescript-json-serializer";

@JsonObject()
class MyClass {
  @JsonProperty({ required: true })
  id: string;
}

const serializer = new JsonSerializer({
  errorCallback: throwError,
});

const myClassInstance = new MyClass();

// myClassInstance.id = "123"

serializer.serialize(myClassInstance);

You can download my sample project here:

typescript-json-serializer-bug.zip

On which OS the bug appears?

macOS 11.3.1

What is your project type?

NodeJS

On which build mode the bug appears?

No response

Anything else?

No response

See related comment: #133 (comment)

Hi, thank you for using my lib :).

I checked and the required option is only checked during the deserialization.
I could add a feature to enable it for the serialization.

Problem is fixed in the new 5.0.0 version!

Exciting! Thank you!