Skip to content

Commit 200eaed

Browse files
authored
docs: add docs for validate-data command and Swift improvements (#218)
Improved content
1 parent 4ead974 commit 200eaed

30 files changed

Lines changed: 428 additions & 185 deletions

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@
2323
<img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="Typesync CLI is released under the MIT license." /></a>
2424
</p>
2525

26-
Typesync is an open-source schema management tool that simplifies managing [Firestore](https://cloud.google.com/firestore) databases. Typesync allows you to maintain a single source of truth for your Firestore architecture in a special _schema_. With this schema in place, you can seamlessly auto-generate type definitions for multiple platforms like TypeScript, Swift, Python, and more using the CLI tool.
26+
Typesync is an open-source schema management tool for [Firestore](https://cloud.google.com/firestore) databases. You maintain a single source of truth for your Firestore architecture in a _schema_, then use the CLI to generate code and validate data from that schema.
2727

28-
Typesync keeps your database and application code consistent and up-to-date at all times. In addition to type definitions, it lets you generate other useful things like Security Rules, Mermaid graphs visualizing your database architecture and documentation for your data models.
28+
Typesync helps keep your database, generated application code, and validation checks aligned as your schema changes.
29+
30+
# How Typesync Helps You
31+
32+
- Generate Firestore models for TypeScript, Swift, and Python from one schema.
33+
- Validate live Firestore documents with `typesync validate-data`.
34+
- Generate Security Rules validators from the same model definitions.
35+
- Visualize your Firestore architecture with Mermaid graphs.
36+
- Carry schema documentation into generated model output.
2937

3038
[**View the full documentation (docs) ▸**](https://docs.typesync.org)
3139

@@ -42,6 +50,7 @@ Explore our comprehensive [documentation](https://docs.typesync.org) for detaile
4250
- [Quickstart](https://docs.typesync.org/quickstart): Get up and running with Typesync quickly.
4351
- [Upgrading](https://docs.typesync.org/upgrading): Guidelines on upgrading to the latest version of the CLI.
4452
- [Types](https://docs.typesync.org/schema/types): Details on the types supported by Typesync’s type system.
53+
- [validate-data](https://docs.typesync.org/cli/validate-data): Validate Firestore documents against your schema.
4554

4655
# License
4756

docs/cli/generate-graph.mdx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
2-
title: "generate-graph"
3-
icon: "rectangle-terminal"
2+
title: 'generate-graph'
3+
icon: 'rectangle-terminal'
44
---
55

6-
import DefinitionOption from "/snippets/cli-option-definition.mdx";
7-
import OutFileOption from "/snippets/cli-option-out-file.mdx";
8-
import IndentationOption from "/snippets/cli-option-indentation.mdx";
9-
import DebugOption from "/snippets/cli-option-debug.mdx";
10-
import ExampleDefinitionGraph from "/snippets/example-definition-graph.mdx";
11-
import ExampleGenerationGraphBefore from "/snippets/example-generation-graph-before.mdx";
12-
import ExampleGenerationGraph from "/snippets/example-generation-graph.mdx";
6+
import DebugOption from '/snippets/cli-option-debug.mdx';
7+
import DefinitionOption from '/snippets/cli-option-definition.mdx';
8+
import IndentationOption from '/snippets/cli-option-indentation.mdx';
9+
import OutFileOption from '/snippets/cli-option-out-file.mdx';
10+
import ExampleDefinitionGraph from '/snippets/example-definition-graph.mdx';
11+
import ExampleGenerationGraphBefore from '/snippets/example-generation-graph-before.mdx';
12+
import ExampleGenerationGraph from '/snippets/example-generation-graph.mdx';
1313

1414
Generates a [Mermaid](https://mermaid.js.org) graph for the specified schema and injects it into the specified Markdown file. The generated graph is the visual representation of the database architecture inferred from your schema. You can specify where the graph is inserted within the file using the `--startMarker` and `--endMarker` options. For a detailed guide, see the full [example](#example) below.
1515

@@ -39,14 +39,9 @@ typesync generate-graph --definition <filePathOrPattern> --outFile <filePath> --
3939
the `<!--` (see [example](#example)).
4040
</ParamField>
4141

42-
<ParamField
43-
type='"vertical" | "horizontal"'
44-
path="orientation"
45-
default="horizontal"
46-
>
47-
The orientation of the generated Mermaid graph. Can be either `"vertical"` or
48-
`"horizontal"` which correspond to the `"TB"` and `"LR"` Mermaid options,
49-
respectively.
42+
<ParamField type='"vertical" | "horizontal"' path="orientation" default="horizontal">
43+
The orientation of the generated Mermaid graph. Can be either `"vertical"` or `"horizontal"` which correspond to the
44+
`"TB"` and `"LR"` Mermaid options, respectively.
5045
</ParamField>
5146

5247
<DebugOption />

docs/cli/generate-py.mdx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
title: "generate-py"
3-
icon: "rectangle-terminal"
2+
title: 'generate-py'
3+
icon: 'rectangle-terminal'
44
---
55

6-
import DefinitionOption from "/snippets/cli-option-definition.mdx";
7-
import OutFileOption from "/snippets/cli-option-out-file.mdx";
8-
import TargetOption from "/snippets/cli-option-target.mdx";
9-
import IndentationOption from "/snippets/cli-option-indentation.mdx";
10-
import DebugOption from "/snippets/cli-option-debug.mdx";
6+
import DebugOption from '/snippets/cli-option-debug.mdx';
7+
import DefinitionOption from '/snippets/cli-option-definition.mdx';
8+
import IndentationOption from '/snippets/cli-option-indentation.mdx';
9+
import OutFileOption from '/snippets/cli-option-out-file.mdx';
10+
import TargetOption from '/snippets/cli-option-target.mdx';
1111

1212
Generates Python/Pydantic type definitions for the specified schema and writes them to the specified file. Typesync first “flattens” your schema, creating new aliases for inline types defined within. It then generates the classes, enums, aliases, etc. including serializers and deserializers.
1313

@@ -34,9 +34,8 @@ Example values:
3434
</ParamField>
3535

3636
<ParamField type="string" path="undefinedSentinelName" default="UNDEFINED">
37-
The name of the sentinel value used to indicate that a field should be missing
38-
from a given object. This is generated as a variable alongside your model
39-
definitions.
37+
The name of the sentinel value used to indicate that a field should be missing from a given object. This is generated
38+
as a variable alongside your model definitions.
4039
</ParamField>
4140

4241
<IndentationOption />

docs/cli/generate-rules.mdx

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
---
2-
title: "generate-rules"
3-
icon: "rectangle-terminal"
2+
title: 'generate-rules'
3+
icon: 'rectangle-terminal'
44
---
55

6-
import DefinitionOption from "/snippets/cli-option-definition.mdx";
7-
import OutFileOption from "/snippets/cli-option-out-file.mdx";
8-
import IndentationOption from "/snippets/cli-option-indentation.mdx";
9-
import DebugOption from "/snippets/cli-option-debug.mdx";
10-
import ExampleDefinition from "/snippets/example-definition.mdx";
11-
import ExampleGenerationRulesBefore from "/snippets/example-generation-rules-before.mdx";
12-
import ExampleGenerationRules from "/snippets/example-generation-rules.mdx";
6+
import DebugOption from '/snippets/cli-option-debug.mdx';
7+
import DefinitionOption from '/snippets/cli-option-definition.mdx';
8+
import IndentationOption from '/snippets/cli-option-indentation.mdx';
9+
import OutFileOption from '/snippets/cli-option-out-file.mdx';
10+
import ExampleDefinition from '/snippets/example-definition.mdx';
11+
import ExampleGenerationRulesBefore from '/snippets/example-generation-rules-before.mdx';
12+
import ExampleGenerationRules from '/snippets/example-generation-rules.mdx';
1313

1414
Generates validator functions for Firestore Security Rules (version 2) and injects them into the specified file. These validators ensure that request data adheres to defined model interfaces and detect any modifications to read-only fields during write operations.
1515

1616
Since Security Rules are centralized in a single file (typically `firestore.rules`), and developers often implement custom rules alongside type validations, Typesync inserts only the necessary validator functions, without overriding your custom rules. You can specify where these validators are added within the file using the `--startMarker` and `--endMarker` options. For a detailed guide, see the full [example](#example) below.
1717

1818
<Info>
19-
Note that these validators must adhere to the intrinsic limitations of
20-
Security Rules. For example, while it's feasible to verify if `x` is a list
21-
with the `x is list` predicate, determining whether it's a list of strings is
22-
not possible since loop constructs are not available in Security Rules.
23-
Typesync will provide the most stringent validation possible within these
24-
constraints.
19+
Note that these validators must adhere to the intrinsic limitations of Security Rules. For example, while it's
20+
feasible to verify if `x` is a list with the `x is list` predicate, determining whether it's a list of strings is not
21+
possible since loop constructs are not available in Security Rules. Typesync will provide the most stringent
22+
validation possible within these constraints.
2523
</Info>
2624

2725
## Usage
@@ -44,10 +42,9 @@ typesync generate-rules --definition <filePathOrPattern> --outFile <filePath> --
4442
</ParamField>
4543

4644
<ParamField type="string" path="endMarker" default="typesync-end">
47-
A marker that indicates the line before which the generated code should be
48-
inserted. Make sure to use a string that is unique within the file. The line
49-
containing the marker must be commented i.e. the marker needs to appear after
50-
the `//` (see [example](#example)).
45+
A marker that indicates the line before which the generated code should be inserted. Make sure to use a string that is
46+
unique within the file. The line containing the marker must be commented i.e. the marker needs to appear after the
47+
`//` (see [example](#example)).
5148
</ParamField>
5249

5350
<ParamField
@@ -68,40 +65,42 @@ typesync generate-rules --definition <filePathOrPattern> --outFile <filePath> --
6865
The name of the parameter taken by each type validator.
6966
</ParamField>
7067

71-
{/* <ParamField
72-
type="string"
73-
path="readonlyFieldValidatorNamePattern"
74-
default="isReadonlyFieldAffectedFor{modelName}"
75-
>
76-
The pattern that specifies how the generated readonly field validators are named. The pattern must be a string that contains the `"{modelName}"` substring (this is a literal value).
77-
78-
Example values:
68+
{/\* <ParamField
69+
type="string"
70+
path="readonlyFieldValidatorNamePattern"
71+
default="isReadonlyFieldAffectedFor{modelName}"
72+
73+
> The pattern that specifies how the generated readonly field validators are named. The pattern must be a string that contains the `"{modelName}"` substring (this is a literal value).
74+
75+
Example values:
7976

8077
- `"isReadonlyFieldAffectedFor{modelName}"` -> produces validators like `isReadonlyFieldAffectedForUser`, `isReadonlyFieldAffectedForProject`, `isReadonlyFieldAffectedForAccount` etc.
8178
- `"affectsReadonlyFieldFor{modelName}"` -> produces validators like `affectsReadonlyFieldForUser`, `affectsReadonlyFieldForProject`, `affectsReadonlyFieldForAccount` etc.
8279

8380
</ParamField> */}
8481

85-
{/* <ParamField
86-
type="string"
87-
path="readonlyFieldValidatorPrevDataParamName"
88-
default="prevData"
89-
>
90-
The name of the first parameter taken by each readonly field validator
91-
representing previous data. This parameter used when computing the diff
92-
between next data and previous data to determine whether a readonly field has
93-
been affected by a write.
82+
{/\* <ParamField
83+
type="string"
84+
path="readonlyFieldValidatorPrevDataParamName"
85+
default="prevData"
86+
87+
> The name of the first parameter taken by each readonly field validator
88+
> representing previous data. This parameter used when computing the diff
89+
> between next data and previous data to determine whether a readonly field has
90+
> been affected by a write.
91+
9492
</ParamField> */}
9593

96-
{/* <ParamField
97-
type="string"
98-
path="readonlyFieldValidatorNextDataParamName"
99-
default="nextData"
100-
>
101-
The name of the second parameter taken by each readonly field validator
102-
representing next data. This parameter used when computing the diff between
103-
next data and previous data to determine whether a readonly field has been
104-
affected by a write.
94+
{/\* <ParamField
95+
type="string"
96+
path="readonlyFieldValidatorNextDataParamName"
97+
default="nextData"
98+
99+
> The name of the second parameter taken by each readonly field validator
100+
> representing next data. This parameter used when computing the diff between
101+
> next data and previous data to determine whether a readonly field has been
102+
> affected by a write.
103+
105104
</ParamField> */}
106105

107106
<IndentationOption />
@@ -116,7 +115,6 @@ Suppose you have a schema definition file named `models.yml` and a Security Rule
116115
<ExampleGenerationRulesBefore />
117116
</CodeGroup>
118117

119-
120118
{/* TODO: Update */}
121119

122120
To generate validators for the defined models and inject them between the `typesync-start` and `typesync-end` markers in the `firestore.rules` file, you can run the following command:

docs/cli/generate-swift.mdx

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
---
2-
title: "generate-swift"
3-
icon: "rectangle-terminal"
2+
title: 'generate-swift'
3+
icon: 'rectangle-terminal'
44
---
55

6-
import DefinitionOption from "/snippets/cli-option-definition.mdx";
7-
import OutFileOption from "/snippets/cli-option-out-file.mdx";
8-
import TargetOption from "/snippets/cli-option-target.mdx";
9-
import IndentationOption from "/snippets/cli-option-indentation.mdx";
10-
import DebugOption from "/snippets/cli-option-debug.mdx";
6+
import DebugOption from '/snippets/cli-option-debug.mdx';
7+
import DefinitionOption from '/snippets/cli-option-definition.mdx';
8+
import IndentationOption from '/snippets/cli-option-indentation.mdx';
9+
import OutFileOption from '/snippets/cli-option-out-file.mdx';
10+
import TargetOption from '/snippets/cli-option-target.mdx';
1111

12-
Generates Swift type definitions for the specified schema and writes them to the specified file. Typesync first "flattens" your schema, creating new aliases for inline types defined within. It then generates the primary and auxiliary structs, enums, aliases, etc. including encoders and decoders.
12+
Generates Swift type definitions for the specified schema and writes them to the specified file. Typesync first "flattens" your schema, creating new aliases for inline types defined within. It then generates the primary and auxiliary structs, enums, aliases, encoders, and decoders.
13+
14+
For document models, generated Swift structs include an `@DocumentID var id: String?` property by default. The Firebase Apple SDK fills this value from the document path when decoding and excludes it from the encoded document body when writing.
1315

1416
## Usage
1517

@@ -28,3 +30,62 @@ typesync generate-swift --definition <filePathOrPattern> --target <targetEnviron
2830
## Targets
2931

3032
- `firebase@10`: For Swift projects that rely on the [Firebase Apple SDK (v10)](https://github.com/firebase/firebase-ios-sdk).
33+
34+
## Document IDs
35+
36+
Document model structs import `FirebaseFirestore` and include an `@DocumentID`
37+
property:
38+
39+
```swift
40+
import FirebaseFirestore
41+
42+
struct User: Codable {
43+
@DocumentID var id: String?
44+
var username: String
45+
}
46+
```
47+
48+
If a document body already has a Firestore field named `id`, rename the generated
49+
document ID property on that document model:
50+
51+
```yaml
52+
User:
53+
model: document
54+
path: users/{userId}
55+
swift:
56+
documentIdProperty:
57+
name: documentId
58+
type:
59+
type: object
60+
fields:
61+
id:
62+
type: string
63+
username:
64+
type: string
65+
```
66+
67+
This produces `@DocumentID var documentId: String?` while keeping the body field
68+
named `id`.
69+
70+
## Swift field names
71+
72+
Use `swift.name` on a field when the generated Swift property name should differ
73+
from the Firestore field name. Typesync keeps encoding and decoding aligned with
74+
the original Firestore key through `CodingKeys`.
75+
76+
```yaml
77+
User:
78+
model: document
79+
path: users/{userId}
80+
type:
81+
type: object
82+
fields:
83+
class:
84+
type: string
85+
swift:
86+
name: userClass
87+
```
88+
89+
Typesync fails generation if two fields resolve to the same Swift property name,
90+
or if a body field conflicts with the generated `@DocumentID` property. Rename
91+
one of the properties with `swift.name` or `swift.documentIdProperty.name`.

docs/cli/generate-ts.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
title: "generate-ts"
3-
icon: "rectangle-terminal"
2+
title: 'generate-ts'
3+
icon: 'rectangle-terminal'
44
---
55

6-
import DefinitionOption from "/snippets/cli-option-definition.mdx";
7-
import OutFileOption from "/snippets/cli-option-out-file.mdx";
8-
import TargetOption from "/snippets/cli-option-target.mdx";
9-
import IndentationOption from "/snippets/cli-option-indentation.mdx";
10-
import DebugOption from "/snippets/cli-option-debug.mdx";
6+
import DebugOption from '/snippets/cli-option-debug.mdx';
7+
import DefinitionOption from '/snippets/cli-option-definition.mdx';
8+
import IndentationOption from '/snippets/cli-option-indentation.mdx';
9+
import OutFileOption from '/snippets/cli-option-out-file.mdx';
10+
import TargetOption from '/snippets/cli-option-target.mdx';
1111

1212
Generates TypeScript type definitions for the specified schema and writes them to the specified file.
1313

0 commit comments

Comments
 (0)