Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Dec 6, 2024
1 parent 9af17c3 commit 259d7c2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/utils/objects/deepMerge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ describe("deepMerge", () => {

expect(alter).toHaveBeenCalledWith("prop", {});
});
it("should alter nested properties", () => {
const alter = vi.fn().mockImplementation((key, value) => {
return key === "nested" ? {...value, extra: true} : value;
});
expect(
deepMerge(
{
prop: {nested: {a: 1}}
},
{
prop: {nested: {b: 2}}
},
{alter}
)
).toEqual({
prop: {nested: {a: 1, b: 2, extra: true}}
});
expect(alter).toHaveBeenCalledWith("nested", {a: 1, b: 2});
});
it("should merge data and prevent prototype pollution", () => {
const obj = JSON.parse('{"__proto__": {"a": "vulnerable"}, "security": [{"1": "o"}, {"2": "o1"}]}');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export function inheritedClassMapper(obj: any, {target, ...options}: JsonSchemaO
const schema = stores.reduce((obj, [, store]) => {
return deepMerge(obj, execMapper("schema", [store.schema], options), {alter: alterMerge});
}, {});
obj = deepMerge(schema, obj, {alter: alterMerge});

return deepMerge(schema, obj, {alter: alterMerge});
}

return obj;
Expand Down
25 changes: 21 additions & 4 deletions packages/specs/schema/src/decorators/common/labelledAs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ import {JsonEntityFn} from "./jsonEntityFn.js";

/**
* Use label to create ref on the current decorated property.
* @param name The name of the label
*
* ### Example
*
* ```ts
* class Model {
* // For single property
* @LabelledAs("UserSchema")
* name: string;
*
* // For collection
* @LabelledAs("UserCollectionSchema", "collection")
* users: User[];
* }
* ```
*
* @param label The name of the label
* @param includeCollection Add the label to the collection. By default, the label is added to the item of the collection it the property is a collection.
* @returns PropertyDecorator
*
* @decorator
*/
export function LabelledAs(name: string, includeCollection: false | "collection" = false): PropertyDecorator {
export function LabelledAs(label: string, includeCollection: false | "collection" = false): PropertyDecorator {
return JsonEntityFn((entity) => {
if (includeCollection) {
entity.schema.label(name);
entity.schema.label(label);
} else {
entity.itemSchema.label(name);
entity.itemSchema.label(label);
}
});
}
9 changes: 8 additions & 1 deletion packages/specs/schema/src/fn/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import {from} from "./from.js";
/**
* Declare a new string model with `format: email`.
*
* * See @@JsonSchema@@ to discover available methods.
* See @@JsonSchema@@ to discover available methods.
*
* ### Example
*
* ```typescript
* const schema = email();
* // Results in: { type: "string", format: "email" }
* ```
*/
export function email(): JsonSchema {
return from(String).format(JsonFormatTypes.EMAIL);
Expand Down
5 changes: 3 additions & 2 deletions packages/specs/schema/src/fn/integer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type {JsonSchema} from "../domain/JsonSchema.js";
import {from} from "./from.js";

/**
* Declare a new integer model.
*
* See @@JsonSchema@@ to discover available methods.
*
* @returns {JsonSchema} A schema configured for integer values
* @function
*/
export function integer() {
export function integer(): JsonSchema {
return from(Number).integer();
}
14 changes: 13 additions & 1 deletion packages/specs/schema/src/fn/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ import {from} from "./from.js";
/**
* Declare a new object model.
*
* * See @@JsonSchema@@ to discover available methods.
* See @@JsonSchema@@ to discover available methods.
*
* ### Example
*
* ```typescript
* const userSchema = object({
* name: string(),
* age: number()
* });
* ```
*
* @param properties - An object containing property definitions where each value is a JsonSchema
* @returns {JsonSchema} A new object model with the specified properties
*/
export function object(properties: {[key: string]: JsonSchema} = {}): JsonSchema {
return from(Object).properties(properties);
Expand Down

0 comments on commit 259d7c2

Please sign in to comment.