Skip to content

Commit 18fb3bf

Browse files
committed
fix(schema): fix Default decorator signature
Closes: #2968
1 parent d33674d commit 18fb3bf

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

packages/specs/schema/src/components/default/schemaMapper.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getValue, isObject} from "@tsed/core";
1+
import {getValue, isFunction, isObject} from "@tsed/core";
22

33
import {mapAliasedProperties} from "../../domain/JsonAliasMap.js";
44
import {JsonSchema} from "../../domain/JsonSchema.js";
@@ -127,6 +127,10 @@ function serializeSchema(schema: JsonSchema, options: JsonSchemaOptions) {
127127
};
128128
}
129129

130+
if (isFunction(obj.default)) {
131+
obj.default = obj.default();
132+
}
133+
130134
obj = execMapper("required", [obj, schema], options);
131135
obj = execMapper("nullable", [obj, schema], options);
132136
obj = alterOneOf(obj, schema, options);

packages/specs/schema/src/decorators/common/default.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,24 @@ describe("@Default", () => {
2222
type: "object"
2323
});
2424
});
25+
it("should declare prop (Date.now)", () => {
26+
// WHEN
27+
class Model {
28+
@Default(Date.now)
29+
num: Date = new Date();
30+
31+
constructor() {}
32+
}
33+
34+
// THEN
35+
expect(getJsonSchema(Model)).toEqual({
36+
properties: {
37+
num: {
38+
default: expect.any(Number),
39+
type: "string"
40+
}
41+
},
42+
type: "object"
43+
});
44+
});
2545
});

packages/specs/schema/src/decorators/common/default.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {JsonEntityFn} from "./jsonEntityFn.js";
4242
* @schema
4343
* @input
4444
*/
45-
export function Default(defaultValue: JSONSchema6Type | undefined) {
45+
export function Default(defaultValue: JSONSchema6Type | undefined | (() => JSONSchema6Type)) {
4646
return JsonEntityFn((store) => {
4747
store.itemSchema.default(defaultValue);
4848
});

packages/specs/schema/src/domain/JsonSchema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export class JsonSchema extends Map<string, any> implements NestedGenerics {
259259
* It is RECOMMENDED that a default value be valid against the associated schema.
260260
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.3
261261
*/
262-
default(value: JSONSchema6Type | undefined) {
262+
default(value: JSONSchema6Type | undefined | (() => JSONSchema6Type)) {
263263
super.set("default", value);
264264

265265
return this;

0 commit comments

Comments
 (0)