@fibery/schema 11.2.0 → 11.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/brands.d.ts CHANGED
@@ -1,20 +1,13 @@
1
+ import { HolderTODO, KindTODO } from "@fibery/helpers/utils/types";
1
2
  //#region src/brands.d.ts
2
- declare const HOLDER: unique symbol;
3
- declare const KIND: unique symbol;
4
- type HolderBrand<H extends string> = {
5
- readonly [HOLDER]?: H;
6
- };
7
- type KindBrand<K extends string> = {
8
- readonly [KIND]?: K;
9
- };
10
- type Id = string & KindBrand<"id">;
11
- type PublicId = string & KindBrand<"publicId">;
12
- type Name = string & KindBrand<"name">;
13
- type TypeId = Id & HolderBrand<"type">;
14
- type FieldId = Id & HolderBrand<"field">;
15
- type EntityId = Id & HolderBrand<"entity">;
16
- type TypeName = Name & HolderBrand<"type">;
17
- type FieldName = Name & HolderBrand<"field">;
18
- type SpaceName = Name & HolderBrand<"space">;
3
+ type Id = string & KindTODO<"id">;
4
+ type PublicId = string & KindTODO<"publicId">;
5
+ type Name = string & KindTODO<"name">;
6
+ type TypeId = Id & HolderTODO<"type">;
7
+ type FieldId = Id & HolderTODO<"field">;
8
+ type EntityId = Id & HolderTODO<"entity">;
9
+ type TypeName = Name & HolderTODO<"type">;
10
+ type FieldName = Name & HolderTODO<"field">;
11
+ type SpaceName = Name & HolderTODO<"space">;
19
12
  //#endregion
20
13
  export { EntityId, FieldId, FieldName, Id, Name, PublicId, SpaceName, TypeId, TypeName };
@@ -1,3 +1,4 @@
1
+ import { FieldId, FieldName, TypeName } from "./brands.js";
1
2
  import { TypeObject } from "./type-object.js";
2
3
  import { SchemaRef } from "./schema.js";
3
4
  //#region src/field-object.d.ts
@@ -21,12 +22,12 @@ type SyncRelation = {
21
22
  type: string;
22
23
  part: string;
23
24
  targetName: string;
24
- targetFieldId: string;
25
+ targetFieldId: FieldId;
25
26
  };
26
27
  type RawFieldObject = {
27
- "fibery/id": string;
28
- "fibery/name": string;
29
- "fibery/type": string;
28
+ "fibery/id": FieldId;
29
+ "fibery/name": FieldName;
30
+ "fibery/type": TypeName;
30
31
  "fibery/description"?: string;
31
32
  "fibery/deleted?"?: boolean;
32
33
  "fibery/meta": {
@@ -88,10 +89,10 @@ declare class FieldObject {
88
89
  constructor({ rawFieldObject, holderType, schema }: FieldObjectParams);
89
90
  toJSON(): string | RawFieldObject;
90
91
  get rawMeta(): Readonly<RawFieldObject["fibery/meta"]>;
91
- get name(): string;
92
+ get name(): FieldName;
92
93
  get description(): string | null;
93
- get id(): string;
94
- get type(): string;
94
+ get id(): FieldId;
95
+ get type(): TypeName;
95
96
  get version(): string;
96
97
  get isDeleted(): boolean;
97
98
  get isCollection(): boolean;
package/lib/index.d.ts CHANGED
@@ -4,5 +4,5 @@ import { RawTypeObject, SyncSource, TypeObject, TypeObjectFactory, TypeObjectPar
4
4
  import { RawSchema, Schema, SchemaRef } from "./schema.js";
5
5
  import { Cardinality, FieldObject, FieldObjectParams, LinkRule, LinkRuleOperand, RawFieldObject, SyncField, SyncRelation } from "./field-object.js";
6
6
  import { Factory, factory, protectiveFactory } from "./factory.js";
7
- import { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets } from "./utils.js";
8
- export { Cardinality, EntityId, Factory, FieldId, FieldName, FieldObject, FieldObjectParams, Id, LinkRule, LinkRuleOperand, Name, NotDefinedGetError, NotFoundError, PublicId, RawFieldObject, RawSchema, RawTypeObject, Schema, SchemaRef, SpaceName, SyncField, SyncRelation, SyncSource, TypeId, TypeName, TypeObject, TypeObjectFactory, TypeObjectParams, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
7
+ import { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets } from "./utils.js";
8
+ export { Cardinality, EntityId, Factory, FieldId, FieldName, FieldObject, FieldObjectParams, Id, LinkRule, LinkRuleOperand, Name, NotDefinedGetError, NotFoundError, PublicId, RawFieldObject, RawSchema, RawTypeObject, Schema, SchemaRef, SpaceName, SyncField, SyncRelation, SyncSource, TypeId, TypeName, TypeObject, TypeObjectFactory, TypeObjectParams, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { NotDefinedGetError, NotFoundError } from "./errors.js";
2
- import { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets } from "./utils.js";
2
+ import { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets } from "./utils.js";
3
3
  import { FieldObject } from "./field-object.js";
4
4
  import { Schema } from "./schema.js";
5
5
  import { TypeObject } from "./type-object.js";
6
6
  import { factory, protectiveFactory } from "./factory.js";
7
- export { FieldObject, NotDefinedGetError, NotFoundError, Schema, TypeObject, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
7
+ export { FieldObject, NotDefinedGetError, NotFoundError, Schema, TypeObject, attachEnumNamespace, capitalizeFirstChar, factory, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, protectiveFactory, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
@@ -1,4 +1,5 @@
1
1
  //#region src/relaxable.d.ts
2
+ declare const relaxableRealm: () => boolean;
2
3
  declare function relaxable(method: string): boolean;
3
4
  //#endregion
4
- export { relaxable };
5
+ export { relaxable, relaxableRealm };
package/lib/relaxable.js CHANGED
@@ -7,4 +7,4 @@ function relaxable(method) {
7
7
  return true;
8
8
  }
9
9
  //#endregion
10
- export { relaxable };
10
+ export { relaxable, relaxableRealm };
@@ -1,4 +1,4 @@
1
- import { FieldId, FieldName } from "./brands.js";
1
+ import { FieldId, FieldName, TypeId, TypeName } from "./brands.js";
2
2
  import { SchemaRef } from "./schema.js";
3
3
  import { FieldObject, FieldObjectParams, RawFieldObject } from "./field-object.js";
4
4
  //#region src/type-object.d.ts
@@ -10,8 +10,8 @@ type SyncSource = {
10
10
  type: string;
11
11
  };
12
12
  type RawTypeObject = {
13
- "fibery/id": string;
14
- "fibery/name": string;
13
+ "fibery/id": TypeId;
14
+ "fibery/name": TypeName;
15
15
  "fibery/description"?: string | null;
16
16
  "fibery/deleted?"?: boolean;
17
17
  "fibery/fields": RawFieldObject[];
@@ -63,9 +63,9 @@ declare class TypeObject {
63
63
  getFieldObjectByIdOrThrow(fieldId: FieldId): FieldObject;
64
64
  getFieldObjectByNameOrThrow(fieldName: FieldName): FieldObject;
65
65
  get rawMeta(): Readonly<RawTypeObject["fibery/meta"]>;
66
- get name(): string;
66
+ get name(): TypeName;
67
67
  get description(): string | null;
68
- get id(): string;
68
+ get id(): TypeId;
69
69
  get isDeleted(): boolean;
70
70
  get version(): string;
71
71
  get isEntityRef(): boolean;
package/lib/utils.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { SyncSource, TypeObject } from "./type-object.js";
1
+ import { RawTypeObject, TypeObject } from "./type-object.js";
2
2
  import { RawSchema } from "./schema.js";
3
- import { RawFieldObject } from "./field-object.js";
4
3
  //#region src/utils.d.ts
5
4
  declare const withOnlyDefinedGets: (target: any, targetKind?: string) => any;
6
5
  declare const capitalizeFirstChar: (input: string) => string;
@@ -28,34 +27,12 @@ declare const attachEnumNamespace: ({ enumNamespace, typeNamePart }: {
28
27
  typeNamePart: string;
29
28
  }) => string;
30
29
  declare const removeDeletedTypesAndFields: (rawSchema: RawSchema) => {
31
- "fibery/types": {
32
- "fibery/fields": RawFieldObject[];
33
- "fibery/id": string;
34
- "fibery/name": string;
35
- "fibery/description"?: string | null;
36
- "fibery/deleted?"?: boolean;
37
- "fibery/meta": {
38
- "fibery/domain?"?: boolean;
39
- "fibery/platform?"?: boolean;
40
- "fibery/highlight?"?: boolean;
41
- "app/mixin?"?: boolean;
42
- "ui/mixin-icon"?: string;
43
- "fibery/enum?"?: boolean;
44
- "fibery/primitive?"?: boolean;
45
- "ui/writer-mode?"?: boolean;
46
- "ui/color"?: string;
47
- "ui/units"?: unknown[];
48
- "app/mixins"?: Record<string, boolean>;
49
- "sync/source"?: SyncSource;
50
- "fibery/secured?"?: boolean;
51
- "fibery/type-component?"?: boolean;
52
- "ref/ref-db?"?: boolean;
53
- "semantic-index/enabled"?: boolean;
54
- };
55
- }[];
30
+ "fibery/types": RawTypeObject[];
56
31
  "fibery/id": string;
57
32
  "fibery/version": string;
58
33
  };
59
34
  declare const getTypeObjectTitle: (typeObject: TypeObject, form?: "singular" | "plural") => string;
35
+ /** `===` does not narrow a branded string (`TypeName`, `FieldName`) to a literal; this does. */
36
+ declare const isOneOf: <const L extends readonly string[]>(value: string, literals: L) => value is L[number];
60
37
  //#endregion
61
- export { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
38
+ export { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
package/lib/utils.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { NotDefinedGetError } from "./errors.js";
2
+ import { relaxableRealm } from "./relaxable.js";
2
3
  import { printJsPath } from "./print-utils.js";
3
4
  import { traceError } from "@fibery/helpers/utils/trace";
4
5
  //#region src/utils.ts
@@ -43,7 +44,11 @@ const capitalizeWord = (str) => str.charAt(0).toUpperCase() + str.slice(1).toLow
43
44
  const toPascalCase = (lispCasedString = "") => lispCasedString && (lispCasedString.split("/").at(-1) || "").split("-").map(capitalizeWord).join(" ");
44
45
  const isObsoleteTitle = (str) => {
45
46
  const { name } = splitKeyword(str);
46
- return name.toLowerCase() === name && !hasTildes(name);
47
+ return startsWithLowercaseLetter(name) && name.toLowerCase() === name && !hasTildes(name);
48
+ };
49
+ const startsWithLowercaseLetter = (str) => {
50
+ const first = str.charAt(0);
51
+ return first !== "" && first !== first.toUpperCase();
47
52
  };
48
53
  const hasTildes = (str) => str.includes("~");
49
54
  const nameOverrides = {
@@ -96,21 +101,42 @@ const makeEnumTypeNamePart = ({ holderType, enumName }) => {
96
101
  const attachEnumNamespace = ({ enumNamespace, typeNamePart }) => {
97
102
  return `${enumNamespace}/${typeNamePart}`;
98
103
  };
104
+ const reportedStrippedFields = /* @__PURE__ */ new Set();
105
+ const reportStrippedField = (holderTypeName, rawFieldObject, isTypeSoftDeleted) => {
106
+ if (!relaxableRealm()) return;
107
+ const key = `${holderTypeName} ${rawFieldObject["fibery/name"]} ${rawFieldObject["fibery/type"]}`;
108
+ if (reportedStrippedFields.has(key)) return;
109
+ reportedStrippedFields.add(key);
110
+ traceError(`Stripped field "${rawFieldObject["fibery/name"]}" of "${holderTypeName}": its type "${rawFieldObject["fibery/type"]}" is ${isTypeSoftDeleted ? "soft-deleted" : "not in schema"}`);
111
+ };
99
112
  const removeDeletedTypesAndFields = (rawSchema) => {
100
- const { "fibery/types": rawTypeObjects, ...schemaRest } = rawSchema;
101
- const filteredRawTypeObjects = rawTypeObjects.filter((rawTypeObject) => rawTypeObject["fibery/deleted?"] !== true).map((rawTypeObject) => {
102
- const { "fibery/fields": rawFieldObjects, ...rawTypeObjectRest } = rawTypeObject;
103
- const filteredRawFieldObjects = rawFieldObjects.filter((rawFieldObject) => rawFieldObject["fibery/deleted?"] !== true);
104
- return {
105
- ...rawTypeObjectRest,
106
- "fibery/fields": filteredRawFieldObjects
107
- };
108
- });
113
+ const rawTypeObjects = rawSchema["fibery/types"];
114
+ const survivingTypeNames = /* @__PURE__ */ new Set();
115
+ const deletedTypeNames = /* @__PURE__ */ new Set();
116
+ for (const rawTypeObject of rawTypeObjects) if (rawTypeObject["fibery/deleted?"] === true) deletedTypeNames.add(rawTypeObject["fibery/name"]);
117
+ else survivingTypeNames.add(rawTypeObject["fibery/name"]);
118
+ const survivingRawTypeObjects = [];
119
+ for (const rawTypeObject of rawTypeObjects) {
120
+ if (rawTypeObject["fibery/deleted?"] === true) continue;
121
+ const rawFieldObjects = rawTypeObject["fibery/fields"];
122
+ const survivingRawFieldObjects = rawFieldObjects.filter((rawFieldObject) => {
123
+ if (rawFieldObject["fibery/deleted?"] === true) return false;
124
+ if (survivingTypeNames.has(rawFieldObject["fibery/type"])) return true;
125
+ reportStrippedField(rawTypeObject["fibery/name"], rawFieldObject, deletedTypeNames.has(rawFieldObject["fibery/type"]));
126
+ return false;
127
+ });
128
+ survivingRawTypeObjects.push(survivingRawFieldObjects.length === rawFieldObjects.length ? rawTypeObject : {
129
+ ...rawTypeObject,
130
+ "fibery/fields": survivingRawFieldObjects
131
+ });
132
+ }
109
133
  return {
110
- ...schemaRest,
111
- "fibery/types": filteredRawTypeObjects
134
+ ...rawSchema,
135
+ "fibery/types": survivingRawTypeObjects
112
136
  };
113
137
  };
114
138
  const getTypeObjectTitle = (typeObject, form) => form ? form === "singular" ? typeObject.singularTitle : typeObject.pluralTitle : typeObject.title;
139
+ /** `===` does not narrow a branded string (`TypeName`, `FieldName`) to a literal; this does. */
140
+ const isOneOf = (value, literals) => literals.includes(value);
115
141
  //#endregion
116
- export { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
142
+ export { attachEnumNamespace, capitalizeFirstChar, getTypeObjectTitle, isOneOf, makeEnumTypeNamePart, parseEnumType, parseType, removeDeletedTypesAndFields, toNonEnumTitle, toPascalCase, toTitle, toTypeOrFieldNamePartTitle, toTypeOrFieldTitle, withOnlyDefinedGets };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/schema",
3
- "version": "11.2.0",
3
+ "version": "11.3.0",
4
4
  "description": "Fibery schema utils",
5
5
  "license": "UNLICENSED",
6
6
  "files": [
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "lodash": "4.17.21",
25
- "@fibery/helpers": "3.3.1",
25
+ "@fibery/helpers": "3.4.0",
26
26
  "@fibery/pluralize": "1.2.1"
27
27
  }
28
28
  }