@@ -6,11 +6,12 @@ description:
66
77# 🏷️ Handle Schemas and Dataset Types
88
9- ** Schemas are like TypeScript for your protected data.** They define the
10- structure and types of your data automatically when you protect it, making it
11- easy for iApps to know what they're working with.
9+ ** Schemas are like content labels that describe what's inside your protected data.**
1210
13- Think of schemas as ** data labels** - they tell iApps "this protected data
11+ They define the structure and types of your data automatically when you protect
12+ it, making it easy for iApps to know what they're working with.
13+
14+ Think of schemas as ** data fingerprints** - they tell iApps "this protected data
1415contains an email address and a phone number" without revealing the actual
1516values.
1617
@@ -20,12 +21,12 @@ When you protect data with DataProtector, the SDK automatically analyzes your
2021JSON object and generates a schema. ** No manual schema definition needed** -
2122it's all handled for you.
2223
23- ``` ts
24+ ``` ts twoslash
2425import { IExecDataProtectorCore , getWeb3Provider } from ' @iexec/dataprotector' ;
2526
2627const web3Provider = getWeb3Provider (' PRIVATE_KEY' );
2728const dataProtectorCore = new IExecDataProtectorCore (web3Provider );
28-
29+ // ---cut---
2930const protectedData = await dataProtectorCore .protectData ({
3031 name: ' User Contact' ,
3132 data: {
@@ -38,20 +39,27 @@ const protectedData = await dataProtectorCore.protectData({
3839 },
3940});
4041
41- // The schema is automatically generated:
42- console .log (protectedData .schema );
43- /* Output:
42+ console .log (' ✅ Protected data created!' );
43+ console .log (' 📍 Address:' , protectedData .address );
44+ ```
45+
46+ ** 🏷️ Generated Schema:**
47+
48+ ``` json
4449{
45- email: ' string' ,
46- phoneNumber: ' string' ,
47- preferences: {
48- newsletter: ' bool' ,
49- notifications: ' bool'
50+ " email" : " string" ,
51+ " phoneNumber" : " string" ,
52+ " preferences" : {
53+ " newsletter" : " bool" ,
54+ " notifications" : " bool"
5055 }
5156}
52- */
5357```
5458
59+ ::: info Schema Structure
60+ The schema automatically maps your data structure to types that iApps can understand and validate.
61+ :::
62+
5563## Supported Data Types
5664
5765The schema automatically detects these types:
@@ -65,16 +73,22 @@ The schema automatically detects these types:
6573| ` application/octet-stream ` | Binary data | File contents |
6674| ` image/jpeg ` , ` image/png ` , etc. | Media files | Images, videos |
6775
68- ::: tip Auto-Detection The SDK automatically detects file types based on
69- content. No need to specify MIME types manually. :::
76+ ::: tip Auto-Detection
77+ The SDK automatically detects file types based on
78+ content. No need to specify MIME types manually.
79+ :::
7080
7181## Why Schemas Matter
7282
7383### 🎯 ** For iApp Development**
7484
7585Schemas let your iApps validate and process data safely:
7686
77- ``` js
87+ ``` ts twoslash
88+ import { IExecDataProtectorDeserializer } from ' @iexec/dataprotector-deserializer' ;
89+
90+ const deserializer = new IExecDataProtectorDeserializer ();
91+ // ---cut---
7892// Inside your iApp
7993const email = await deserializer .getValue (' email' , ' string' );
8094const preferences = await deserializer .getValue (
@@ -83,38 +97,64 @@ const preferences = await deserializer.getValue(
8397);
8498```
8599
100+ ### 🛡️ ** For Type Safety**
101+
102+ Prevents your iApps from processing incompatible data types.
103+
86104### 🔍 ** For Data Discovery**
87105
88106Users can find relevant protected data without seeing the actual content:
89107
90- ``` ts
91- // Find all protected data with email addresses
92- const query = { schema: { email: ' string' } };
93- // Returns metadata only, no actual emails revealed
94- ```
95-
96- ### 🛡️ ** For Type Safety**
108+ ``` ts twoslash
109+ import { IExecDataProtectorCore , getWeb3Provider } from ' @iexec/dataprotector' ;
97110
98- Prevents your iApps from processing incompatible data types.
111+ const web3Provider = getWeb3Provider (' PRIVATE_KEY' );
112+ const dataProtectorCore = new IExecDataProtectorCore (web3Provider );
113+ // ---cut---
114+ const listProtectedData = await dataProtectorCore .getProtectedData ({
115+ requiredSchema: {
116+ email: ' string' ,
117+ },
118+ });
119+ ```
99120
100121## Real Examples
101122
102123### Simple User Profile
103124
104- ``` ts
125+ ``` ts twoslash
126+ import { IExecDataProtectorCore , getWeb3Provider } from ' @iexec/dataprotector' ;
127+
128+ const web3Provider = getWeb3Provider (' PRIVATE_KEY' );
129+ const dataProtectorCore = new IExecDataProtectorCore (web3Provider );
130+ // ---cut---
105131const userData = await dataProtectorCore .protectData ({
106132 data: {
107133108134 age: 25 ,
109135 isSubscribed: true ,
110136 },
111137});
112- // Schema: { email: 'string', age: 'f64', isSubscribed: 'bool' }
138+ ```
139+
140+ ** 🏷️ Generated Schema:**
141+
142+ ``` json
143+ {
144+ "email" : " string" ,
145+ "age" : " f64" ,
146+ "isSubscribed" : " bool"
147+ }
113148```
114149
115150### Nested Contact Information
116151
117- ``` ts
152+ ``` ts twoslash
153+ import { IExecDataProtectorCore , getWeb3Provider } from ' @iexec/dataprotector' ;
154+
155+ const web3Provider = getWeb3Provider (' PRIVATE_KEY' );
156+ const dataProtectorCore = new IExecDataProtectorCore (web3Provider );
157+ // ---cut---
118158const contactData = await dataProtectorCore .protectData ({
119159 data: {
120160 personal: {
@@ -131,15 +171,43 @@ const contactData = await dataProtectorCore.protectData({
131171 },
132172 },
133173});
134- // Schema reflects the full nested structure
174+ ```
175+
176+ ** 🏷️ Generated Schema:**
177+
178+ ``` json
179+ {
180+ "personal" : {
181+ "firstName" : " string" ,
182+ "lastName" : " string"
183+ },
184+ "contact" : {
185+ "email" : " string" ,
186+ "phone" : " string"
187+ },
188+ "preferences" : {
189+ "marketing" : " bool" ,
190+ "notifications" : " bool"
191+ }
192+ }
135193```
136194
137195### File Data
138196
139- ``` ts
140- import { createArrayBufferFromFile } from ' @iexec/dataprotector' ;
197+ ``` ts twoslash
198+ import { IExecDataProtectorCore , getWeb3Provider } from ' @iexec/dataprotector' ;
199+
200+ // Mock function for the example
201+ function createArrayBufferFromFile(file : File ): Promise <ArrayBuffer > {
202+ return Promise .resolve (new ArrayBuffer (0 ));
203+ }
204+
205+ // Get file from input element
206+ const file = new File ([" " ], " example.jpg" , { type: " image/jpeg" });
141207
142- const file = document .getElementById (' fileInput' ).files [0 ];
208+ const web3Provider = getWeb3Provider (' PRIVATE_KEY' );
209+ const dataProtectorCore = new IExecDataProtectorCore (web3Provider );
210+ // ---cut---
143211const fileBuffer = await createArrayBufferFromFile (file );
144212
145213const fileData = await dataProtectorCore .protectData ({
@@ -149,20 +217,31 @@ const fileData = await dataProtectorCore.protectData({
149217 uploadDate: Date .now (),
150218 },
151219});
152- // Schema: { fileName: 'string', fileContent: 'image/jpeg', uploadDate: 'f64' }
220+ ```
221+
222+ ** 🏷️ Schema for file upload:**
223+
224+ ``` json
225+ {
226+ "fileName" : " string" ,
227+ "fileContent" : " image/jpeg" ,
228+ "uploadDate" : " f64"
229+ }
153230```
154231
155232## Using Schemas in iApps
156233
157234Once you have protected data with a schema, you'll want to process it inside an
158235iApp.
159236
160- ::: warning Type Matching ** Your iApp and frontend must use the same field names
237+ ::: warning Type Matching
238+ ** Your iApp and frontend must use the same field names
161239and types.** If they don't match, you'll get runtime errors when processing the
162- data. :::
240+ data.
241+ :::
163242
164243→ ** Ready to build an iApp?** Check out our detailed
165- [ Inputs and Outputs guide] ( /build_iapp /guides/inputs-and-outputs ) to learn how
244+ [ Inputs and Outputs guide] ( /build-iapp /guides/inputs-and-outputs ) to learn how
166245to access schema fields inside your iApp using the deserializer.
167246
168247## Next Steps
@@ -171,16 +250,10 @@ to access schema fields inside your iApp using the deserializer.
171250explore next:
172251
173252- ** Build an iApp** : Check out the
174- [ iApp Generator guide] ( /build_iapp /iapp-generator ) to create your first data
253+ [ iApp Generator guide] ( /build-iapp /iapp-generator ) to create your first data
175254 processor
176255- ** Process data** : Learn about
177256 [ processProtectedData] ( /manage-data/dataProtector/dataProtectorCore/processProtectedData )
178257 for running computations
179258- ** See it in action** : Try our [ Hello World tutorial] ( /overview/helloWorld ) for
180259 a complete example
181-
182- ---
183-
184- ** TL;DR** : Schemas = auto-generated data labels. Frontend protects data → Schema
185- describes structure → iApp uses schema to access fields safely. Match your field
186- names and types between frontend and iApp! 🏷️
0 commit comments