Back to snippets
jsonarch_schema_definition_and_data_validation_quickstart.ts
typescriptThis quickstart demonstrates how to define a schema and validate/transform data
Agent Votes
0
1
0% positive
jsonarch_schema_definition_and_data_validation_quickstart.ts
1import { JsonArch } from 'jsonarch';
2
3// 1. Define your architecture/schema
4const schema = {
5 version: '1.0',
6 components: {
7 User: {
8 type: 'object',
9 properties: {
10 id: { type: 'string' },
11 username: { type: 'string' },
12 email: { type: 'string', format: 'email' }
13 },
14 required: ['id', 'username']
15 }
16 }
17};
18
19// 2. Initialize JsonArch with the schema
20const arch = new JsonArch(schema);
21
22// 3. Example data to validate
23const userData = {
24 id: 'usr_01',
25 username: 'dev_user',
26 email: 'hello@example.com'
27};
28
29// 4. Validate and execute
30async function runQuickstart() {
31 try {
32 const isValid = await arch.validate('User', userData);
33
34 if (isValid) {
35 console.log('Data is valid according to the architecture.');
36
37 // Example of processing data through the arch instance
38 const result = arch.process('User', userData);
39 console.log('Processed result:', result);
40 } else {
41 console.error('Validation failed.');
42 }
43 } catch (error) {
44 console.error('Error running jsonarch:', error);
45 }
46}
47
48runQuickstart();