Skip to content

Commit 6b7e5e7

Browse files
Feat: Rebuilding getting started component (#687)
* adding componet * adding data * connecting with data * Pushed data changes * Fix:getting started data fetching problem * updating component rendering * adjusting layout * update: changing interating logics * update: changing file location * fix: minor fixes * schema data fetching * fixing workflow * adding fetch testing * replacing code editor * feat: enabling instnaces and moving to new component * fix: state rendering problem solved * feat:adding text and icons * fix: instances fix * fixing text alignment * fix: instances fix * Refactoring and improvements * removing some codes * Refactoring component and bug fixes * Add plausible events * Update package.json * adding better messaging --------- Co-authored-by: Benjamin Granados <[email protected]>
1 parent e796a69 commit 6b7e5e7

File tree

12 files changed

+453
-23
lines changed

12 files changed

+453
-23
lines changed

components/GettingStarted.tsx

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
import React, { useState, useEffect } from 'react';
2+
import { atomOneDark } from 'react-syntax-highlighter/dist/cjs/styles/hljs';
3+
import Highlight from 'react-syntax-highlighter';
4+
5+
async function fetchData() {
6+
const response = await fetch('/data/getting-started-examples.json');
7+
const data = await response.json();
8+
9+
const defaultSchemaData = data.find((data: any) => data.default === true);
10+
11+
const schemaResp = await fetch(defaultSchemaData.file);
12+
const schemaData = await schemaResp.json();
13+
14+
const defaultInstanceData = defaultSchemaData.instances.find(
15+
(instance: any) => instance.default === true,
16+
);
17+
18+
console.log(defaultInstanceData);
19+
20+
const instanceResp = await fetch(defaultInstanceData.file);
21+
const instanceData = await instanceResp.json();
22+
23+
return {
24+
data,
25+
schemaData,
26+
instanceData,
27+
initialInstance: defaultSchemaData.instances,
28+
initialDetails: [defaultInstanceData.details, defaultInstanceData.valid],
29+
};
30+
}
31+
interface SchemaOption {
32+
file: string;
33+
instances: InstanceOption[];
34+
}
35+
36+
interface InstanceOption {
37+
file: string;
38+
details: string;
39+
valid: string;
40+
}
41+
42+
const GettingStarted = () => {
43+
useEffect(() => {
44+
fetchData().then(
45+
({ data, schemaData, instanceData, initialInstance, initialDetails }) => {
46+
setOptions(data);
47+
setFetchedSchema(schemaData);
48+
setFetchedInstance(instanceData);
49+
setInstances(initialInstance);
50+
setDetails(initialDetails);
51+
},
52+
);
53+
}, []);
54+
55+
const [options, setOptions] = useState<SchemaOption[]>([]);
56+
const [instances, setInstances] = useState<InstanceOption[]>([]);
57+
const [details, setDetails] = useState<string[]>(['', '']);
58+
59+
const [fetchedSchema, setFetchedSchema] = useState();
60+
const [fetchedInstance, setFetchedInstance] = useState();
61+
62+
const handleSchemaChange = async (
63+
e: React.ChangeEvent<HTMLSelectElement>,
64+
) => {
65+
const selectedSchema = options.find(
66+
(schema) => schema.file === e.target.value,
67+
);
68+
69+
if (selectedSchema) {
70+
const schemaResponse = await fetch(e.target.value);
71+
const schemaData = await schemaResponse.json();
72+
setFetchedSchema(schemaData);
73+
setInstances(selectedSchema.instances);
74+
75+
const instResp = await fetch(selectedSchema.instances[0].file);
76+
const instData = await instResp.json();
77+
setFetchedInstance(instData);
78+
} else {
79+
setInstances([]);
80+
setFetchedSchema(null!);
81+
}
82+
};
83+
84+
const handleInstanceChange = async (
85+
e: React.ChangeEvent<HTMLSelectElement>,
86+
) => {
87+
const selectedInstance = instances.find(
88+
(instance) => instance.file === e.target.value,
89+
);
90+
91+
if (selectedInstance) {
92+
const instanceResponse = await fetch(e.target.value);
93+
const instanceData = await instanceResponse.json();
94+
setFetchedInstance(instanceData);
95+
setDetails([selectedInstance.details, selectedInstance.valid]);
96+
} else {
97+
setFetchedInstance(null!);
98+
}
99+
};
100+
101+
return (
102+
<>
103+
<div className='flex flex-col'>
104+
<div className='flex items-end flex-row justify-between mt-5 mb-3 '>
105+
<h2 className='text-h5 font-semibold mb-1'>JSON Schema</h2>
106+
<div className='select-wrap'>
107+
<label className='mr-2'>Select a Schema:</label>
108+
<select
109+
name='Select a JSON Schema Validator'
110+
className='p-2 border dark:border-slate-300 border-slate-800 dark:bg-slate-900 rounded-md max-sm:text-[12px] plausible-event-name==activation-explore-tools'
111+
id='Examples'
112+
onChange={handleSchemaChange}
113+
>
114+
{options.map((option: any, id: number) => (
115+
<option key={id} value={option.file}>
116+
{option.name}
117+
</option>
118+
))}
119+
</select>
120+
</div>
121+
</div>
122+
123+
<div className='overflow-x-auto flex-basis-0 max-w-full min-w-0 shrink lg:max-w-[800px] xl:max-w-[900px]'>
124+
<Highlight
125+
wrapLines={true}
126+
wrapLongLines={true}
127+
customStyle={{
128+
borderRadius: 10,
129+
paddingTop: 15,
130+
paddingBottom: 10,
131+
paddingLeft: 10,
132+
marginBottom: 20,
133+
maxWidth: '100%',
134+
}}
135+
lineNumberStyle={{
136+
marginRight: 10,
137+
}}
138+
style={atomOneDark}
139+
showLineNumbers
140+
startingLineNumber={1}
141+
lineProps={() => {
142+
const isHighlighted = false;
143+
return {
144+
className: `${isHighlighted ? 'bg-code-editor-dark-highlight block ml-10 w-full' : ''} pr-8`,
145+
};
146+
}}
147+
codeTagProps={{
148+
className: 'mr-8',
149+
}}
150+
>
151+
{JSON.stringify(fetchedSchema, null, 2)}
152+
</Highlight>
153+
</div>
154+
</div>
155+
156+
<div className='flex flex-col'>
157+
<div className='flex items-end flex-row justify-between mt-5 mb-3 '>
158+
<h2 className='text-h5 font-semibold mb-1'>JSON Instance</h2>
159+
<div className='select-wrap'>
160+
<label className='mr-2'>Select an Instance:</label>
161+
<select
162+
name='Select a JSON Schema Validator'
163+
className='p-2 border dark:border-slate-300 border-slate-800 dark:bg-slate-900 rounded-md max-sm:text-[12px] plausible-event-name==activation-explore-tools'
164+
id='Examples'
165+
onChange={handleInstanceChange}
166+
>
167+
{instances.map((instance: any, id: number) => (
168+
<option key={id} value={instance.file}>
169+
{instance.name}
170+
</option>
171+
))}
172+
</select>
173+
</div>
174+
</div>
175+
<div className='overflow-x-auto flex-basis-0 max-w-full min-w-0 shrink lg:max-w-[800px] xl:max-w-[900px]'>
176+
<Highlight
177+
wrapLines={true}
178+
wrapLongLines={true}
179+
customStyle={{
180+
borderRadius: 10,
181+
paddingTop: 15,
182+
paddingBottom: 10,
183+
paddingLeft: 10,
184+
marginBottom: 20,
185+
maxWidth: '100%',
186+
}}
187+
lineNumberStyle={{
188+
marginRight: 10,
189+
}}
190+
style={atomOneDark}
191+
showLineNumbers
192+
startingLineNumber={1}
193+
lineProps={() => {
194+
const isHighlighted = false;
195+
return {
196+
className: `${isHighlighted ? 'bg-code-editor-dark-highlight block ml-10 w-full' : ''} pr-8`,
197+
};
198+
}}
199+
codeTagProps={{
200+
className: 'mr-8',
201+
}}
202+
>
203+
{JSON.stringify(fetchedInstance, null, 2)}
204+
</Highlight>
205+
</div>
206+
<h2 className='text-h5 font-semibold'>Validation Result</h2>
207+
<div className='flex bg-[#282c34] justify-between items-center text-white font-medium flex-row border p-5 rounded-xl'>
208+
<p>{details[0]}</p>
209+
210+
{details[1] ? (
211+
<img src='/icons/green-tick.svg' alt='green tick' />
212+
) : (
213+
<img src='/icons/red-cross.svg' alt='red cross' />
214+
)}
215+
</div>
216+
</div>
217+
</>
218+
);
219+
};
220+
221+
export default GettingStarted;

pages/learn/getting-started-step-by-step.md renamed to pages/learn/getting-started-step-by-step/getting-started-step-by-step.md

+8-23
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ title: Creating your first schema
33
section: docs
44
---
55

6-
JSON Schema is a vocabulary that you can use to annotate and validate JSON documents. This tutorial guides you through the process of creating a JSON Schema document.
6+
JSON Schema is a vocabulary that you can use to annotate and validate JSON documents. This tutorial guides you through the process of creating a JSON Schema.
77

8-
After you create the JSON Schema document, you can validate the example data against your schema using a validator in a language of your choice. See <userevent type='plausible-event-name=activation-explore-tools'>[Tools](https://json-schema.org/implementations)</userevent> for a current list of supported validators.
8+
After creating your JSON Schema, you can then validate example data against your schema by using a validator in a language of your choice. Please, visit <userevent type='plausible-event-name=activation-explore-tools'>[Tools](https://json-schema.org/implementations#validators)</userevent> and select the validator that better suit your needs.
9+
10+
If you already know how to create JSON Schemas and you are looking for different JSON Schema use cases like schema generation, code generation, documentation, UI generation or JSON Schema processing or conversion, please visit <userevent type='plausible-event-name=activation-explore-tools'>[Tools](https://json-schema.org/implementations)</userevent> and explore the amazing tooling available in the JSON Schema Ecosystem.
911

1012
<tableofcontent depth={2} />
1113

@@ -573,28 +575,11 @@ With the external schema reference, the overall schema looks like this:
573575

574576
## Validate JSON data against the schema
575577

576-
This section describes how to validate JSON data against the product catalog schema.
578+
Now that you have your JSON Schema is time to validate [JSON data](https://json-schema.org/learn/glossary#instance) against it using a <userevent type='plausible-event-name=activation-explore-tools'>[JSON Schema Validator](https://json-schema.org/implementations#validators)</userevent>.
577579

578-
This example JSON data matches the product catalog schema:
580+
A Validator is a tool that implements the JSON Schema specification. All validators works in a similar way: they take a JSON Schema and a JSON Instance as input and they returns the validation result as output.
579581

580-
```json
581-
{
582-
"productId": 1,
583-
"productName": "An ice sculpture",
584-
"price": 12.50,
585-
"tags": [ "cold", "ice" ],
586-
"dimensions": {
587-
"length": 7.0,
588-
"width": 12.0,
589-
"height": 9.5
590-
},
591-
"warehouseLocation": {
592-
"latitude": -78.75,
593-
"longitude": 20.4
594-
}
595-
}
596-
```
582+
![How JSON Schema works](https://json-schema.org/img/json_schema.svg)
597583

598-
To validate this JSON data against the product catalog JSON Schema, you can use any validator of your choice. In addition to command-line and browser tools, validation tools are available in a wide range of languages, including Java, Python, .NET, and many others. To find a validator that’s right for your project, see <userevent type='plausible-event-name=activation-explore-tools'>[Tools](https://json-schema.org/implementations)</userevent>.
584+
To try it yourself, please visit <userevent type='plausible-event-name=activation-explore-tools'>[Tools](https://json-schema.org/implementations#validators)</userevent> and select the validator that better suit your needs, our use the editors available below and select the different Schemas and Instances and see the different validation results.
599585

600-
Use the example JSON data as the input data and the product catalog JSON Schema as the schema. Your validation tool compares the data against the schema, and if the data meets all the requirements defined in the schema, validation is successful.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import fs from 'fs';
3+
4+
import { getLayout } from '~/components/Sidebar';
5+
import Head from 'next/head';
6+
import { Headline1 } from '~/components/Headlines';
7+
import matter from 'gray-matter';
8+
import StyledMarkdown from '~/components/StyledMarkdown';
9+
import { SectionContext } from '~/context';
10+
import { DocsHelp } from '~/components/DocsHelp';
11+
import GettingStarted from '~/components/GettingStarted';
12+
13+
export async function getStaticProps() {
14+
const block1 = fs.readFileSync(
15+
'pages/learn/getting-started-step-by-step/getting-started-step-by-step.md',
16+
'utf-8',
17+
);
18+
const block2 = fs.readFileSync(
19+
'pages/learn/getting-started-step-by-step/next-steps.md',
20+
'utf-8',
21+
);
22+
const { content: block1Content } = matter(block1);
23+
const { content: block2Content } = matter(block2);
24+
return {
25+
props: {
26+
blocks: [block1Content, block2Content],
27+
},
28+
};
29+
}
30+
31+
export default function StyledValidator({ blocks }: { blocks: any[] }) {
32+
const newTitle = 'Creating your first schema';
33+
34+
return (
35+
<SectionContext.Provider value='docs'>
36+
<Head>
37+
<title>{newTitle}</title>
38+
</Head>
39+
<Headline1>{newTitle}</Headline1>
40+
<StyledMarkdown markdown={blocks[0]} />
41+
<GettingStarted />
42+
<StyledMarkdown markdown={blocks[1]} />
43+
<DocsHelp />
44+
</SectionContext.Provider>
45+
);
46+
}
47+
StyledValidator.getLayout = getLayout;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Getting Started Next Steps
3+
section: docs
4+
---
5+
6+
## What Next?
7+
8+
Now that you know how to create a JSON Schema and use it to validate JSON data, we'd invite you to continue your JSON Schema journey:
9+
* Learn more about JSON Schema by visiting the [reference documentation](../understanding-json-schema).
10+
* Explore the details of the current version of the Spec [2020-12](https://json-schema.org/specification).
11+
12+
If you already know how to create JSON Schemas and you are looking for different JSON Schema use cases like schema generation, code generation, documentation, UI generation or JSON Schema processing or conversion, please visit <userevent type='plausible-event-name=activation-explore-tools'>[Tools](https://json-schema.org/implementations)</userevent> and explore the amazing tooling available in the JSON Schema Ecosystem.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"name": "Getting started basic schema",
4+
"default": true,
5+
"file": "/data/getting-started-examples/schemas/default.json",
6+
"instances": [
7+
{
8+
"name": "Valid instance",
9+
"default": true,
10+
"valid": true,
11+
"file": "/data/getting-started-examples/instances/default-ok.json",
12+
"details": "This is a valid JSON instance for the provided JSON Schema"
13+
},
14+
{
15+
"name": "Invalid instance",
16+
"default": false,
17+
"valid": false,
18+
"file": "/data/getting-started-examples/instances/default-ko.json",
19+
"details": "Invalid: The value of 'price' property must be a number"
20+
}
21+
]
22+
},
23+
24+
{
25+
"name": "Getting started extended schema",
26+
"default": false,
27+
"file": "/data/getting-started-examples/schemas/default-extended.json",
28+
"instances": [
29+
{
30+
"name": "Valid instance",
31+
"default": true,
32+
"valid": true,
33+
"file": "/data/getting-started-examples/instances/default-extended-ok.json",
34+
"details": "This is a valid JSON instance for the provided JSON Schema"
35+
}
36+
]
37+
}
38+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"productId": 1,
3+
"productName": "An ice sculpture",
4+
"price": 12.5,
5+
"tags": ["cold", "ice"],
6+
"dimensions": {
7+
"length": 7.0,
8+
"width": 12.0,
9+
"height": 9.5
10+
},
11+
"warehouseLocation": {
12+
"latitude": -78.75,
13+
"longitude": 20.4
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"productId": 1,
3+
"product": "An ice sculpture",
4+
"price": "100"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"productId": 1,
3+
"productName": "An ice sculpture",
4+
"price": 12.5,
5+
"tags": ["cold", "ice"]
6+
}

0 commit comments

Comments
 (0)