Skip to content

Commit

Permalink
fix parsing of 'string<>' type
Browse files Browse the repository at this point in the history
It used to think it was optional because it ends up being merged with
'mixed<>' which is implicitly optional, and we weren't explicitly setting
optional=false.
  • Loading branch information
RedBeard0531 committed Nov 15, 2022
1 parent b58ec2b commit a284fd9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/realm/src/schema/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function normalizePropertyType(type: string, allowObjectType = true): Obj
return {
type: "list",
objectType: item.type === "object" ? item.objectType : item.type,
optional: item.type === "object" ? false : item.optional,
optional: item.type === "object" ? false : !!item.optional,
};
} else if (type.endsWith("<>")) {
assert(allowObjectType, "Expected no 'objectType' in property schema, when using '<>' shorthand");
Expand All @@ -161,7 +161,7 @@ export function normalizePropertyType(type: string, allowObjectType = true): Obj
return {
type: "set",
objectType: item.type === "object" ? item.objectType : item.type,
optional: item.type === "object" ? true : item.optional,
optional: item.type === "object" ? true : !!item.optional,
};
} else if (type.endsWith("{}")) {
assert(allowObjectType, "Expected no 'objectType' in property schema, when using '{}' shorthand");
Expand All @@ -171,7 +171,7 @@ export function normalizePropertyType(type: string, allowObjectType = true): Obj
return {
type: "dictionary",
objectType: item.type === "object" ? item.objectType : item.type,
optional: item.type === "object" ? true : item.optional,
optional: item.type === "object" ? true : !!item.optional,
};
} else if (type.endsWith("?")) {
return {
Expand All @@ -183,6 +183,8 @@ export function normalizePropertyType(type: string, allowObjectType = true): Obj
return { type, objectType: "mixed", optional: true };
} else if (type === "set") {
return { type, objectType: "mixed", optional: true };
} else if (type === "mixed") {
return {type, optional: true};
} else {
// This type is directly mappable, so it can't be the name a user defined object schema.
return { type };
Expand Down
8 changes: 8 additions & 0 deletions packages/realm/src/tests/schema-normalization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ describe("normalizePropertySchema", () => {
expect(result.optional).equals(false);
});

it("transforms a string declaring a set of strings", () => {
const result = normalizePropertySchema("prop", "string<>");
expect(result.name).equals("prop");
expect(result.type).equals("set");
expect(result.objectType).equals("string");
expect(result.optional).equals(false);
});

it("transforms a string declaring a list of optional strings", () => {
const result = normalizePropertySchema("prop", "string?[]");
expect(result.name).equals("prop");
Expand Down

0 comments on commit a284fd9

Please sign in to comment.