Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Rebuilding getting started component #687

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9a8ec04
adding componet
DhairyaMajmudar Apr 20, 2024
fbfe7cb
adding data
DhairyaMajmudar Apr 20, 2024
4a1930d
connecting with data
DhairyaMajmudar Apr 20, 2024
440e8dd
Pushed data changes
benjagm May 16, 2024
35d83fa
Fix:getting started data fetching problem
DhairyaMajmudar May 29, 2024
b20f37d
updating component rendering
DhairyaMajmudar May 29, 2024
4d79def
adjusting layout
DhairyaMajmudar May 30, 2024
1979383
update: changing interating logics
DhairyaMajmudar Jun 5, 2024
77c4326
update: changing file location
DhairyaMajmudar Jun 5, 2024
fc74f50
fix: minor fixes
DhairyaMajmudar Jun 5, 2024
1071cf3
schema data fetching
DhairyaMajmudar Jun 6, 2024
cd0f527
fixing workflow
DhairyaMajmudar Jun 6, 2024
6cca80d
adding fetch testing
DhairyaMajmudar Jun 6, 2024
946f7b0
replacing code editor
DhairyaMajmudar Jun 7, 2024
bde43c5
feat: enabling instnaces and moving to new component
DhairyaMajmudar Jun 7, 2024
8b01252
fix: state rendering problem solved
DhairyaMajmudar Jun 7, 2024
24293fe
feat:adding text and icons
DhairyaMajmudar Jun 7, 2024
a60d761
fix: instances fix
DhairyaMajmudar Jun 18, 2024
1f60401
fixing text alignment
DhairyaMajmudar Jun 18, 2024
0b6f3d0
fix: instances fix
DhairyaMajmudar Jun 20, 2024
e7e6574
Refactoring and improvements
benjagm Jun 20, 2024
8de746c
removing some codes
DhairyaMajmudar Jun 20, 2024
e7bc1be
Refactoring component and bug fixes
DhairyaMajmudar Jun 22, 2024
1aa7b3d
Add plausible events
benjagm Jun 24, 2024
7979703
Update package.json
benjagm Jun 24, 2024
8345cd3
adding better messaging
benjagm Jun 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions components/GettingStarted.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
import React, { useState, useEffect } from 'react';
import { atomOneDark } from 'react-syntax-highlighter/dist/cjs/styles/hljs';
import Highlight from 'react-syntax-highlighter';

async function fetchData() {
const response = await fetch('/data/getting-started-examples.json');
const data = await response.json();

const defaultSchemaData = data.find((data: any) => data.default === true);

const schemaResp = await fetch(defaultSchemaData.file);
const schemaData = await schemaResp.json();

const defaultInstanceData = defaultSchemaData.instances.find(
(instance: any) => instance.default === true,
);

console.log(defaultInstanceData);

const instanceResp = await fetch(defaultInstanceData.file);
const instanceData = await instanceResp.json();

return {
data,
schemaData,
instanceData,
initialInstance: defaultSchemaData.instances,
initialDetails: [defaultInstanceData.details, defaultInstanceData.valid],
};
}
interface SchemaOption {
file: string;
instances: InstanceOption[];
}

interface InstanceOption {
file: string;
details: string;
valid: string;
}

const GettingStarted = () => {
useEffect(() => {
fetchData().then(
({ data, schemaData, instanceData, initialInstance, initialDetails }) => {
setOptions(data);
setFetchedSchema(schemaData);
setFetchedInstance(instanceData);
setInstances(initialInstance);
setDetails(initialDetails);
},
);
}, []);

const [options, setOptions] = useState<SchemaOption[]>([]);
const [instances, setInstances] = useState<InstanceOption[]>([]);
const [details, setDetails] = useState<string[]>(['', '']);

const [fetchedSchema, setFetchedSchema] = useState();
const [fetchedInstance, setFetchedInstance] = useState();

const handleSchemaChange = async (
e: React.ChangeEvent<HTMLSelectElement>,
) => {
const selectedSchema = options.find(
(schema) => schema.file === e.target.value,
);

if (selectedSchema) {
const schemaResponse = await fetch(e.target.value);
const schemaData = await schemaResponse.json();
setFetchedSchema(schemaData);
setInstances(selectedSchema.instances);

const instResp = await fetch(selectedSchema.instances[0].file);
const instData = await instResp.json();
setFetchedInstance(instData);
} else {
setInstances([]);
setFetchedSchema(null!);
}
};

const handleInstanceChange = async (
e: React.ChangeEvent<HTMLSelectElement>,
) => {
const selectedInstance = instances.find(
(instance) => instance.file === e.target.value,
);

if (selectedInstance) {
const instanceResponse = await fetch(e.target.value);
const instanceData = await instanceResponse.json();
setFetchedInstance(instanceData);
setDetails([selectedInstance.details, selectedInstance.valid]);
} else {
setFetchedInstance(null!);
}
};

return (
<>
<div className='flex flex-col'>
<div className='flex items-end flex-row justify-between mt-5 mb-3 '>
<h2 className='text-h5 font-semibold mb-1'>JSON Schema</h2>
<div className='select-wrap'>
<label className='mr-2'>Select a Schema:</label>
<select
name='Select a JSON Schema Validator'
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'
id='Examples'
onChange={handleSchemaChange}
>
{options.map((option: any, id: number) => (
<option key={id} value={option.file}>
{option.name}
</option>
))}
</select>
</div>
</div>

<div className='overflow-x-auto flex-basis-0 max-w-full min-w-0 shrink lg:max-w-[800px] xl:max-w-[900px]'>
<Highlight
wrapLines={true}
wrapLongLines={true}
customStyle={{
borderRadius: 10,
paddingTop: 15,
paddingBottom: 10,
paddingLeft: 10,
marginBottom: 20,
maxWidth: '100%',
}}
lineNumberStyle={{
marginRight: 10,
}}
style={atomOneDark}
showLineNumbers
startingLineNumber={1}
lineProps={() => {
const isHighlighted = false;
return {
className: `${isHighlighted ? 'bg-code-editor-dark-highlight block ml-10 w-full' : ''} pr-8`,
};
}}
codeTagProps={{
className: 'mr-8',
}}
>
{JSON.stringify(fetchedSchema, null, 2)}
</Highlight>
</div>
</div>

<div className='flex flex-col'>
<div className='flex items-end flex-row justify-between mt-5 mb-3 '>
<h2 className='text-h5 font-semibold mb-1'>JSON Instance</h2>
<div className='select-wrap'>
<label className='mr-2'>Select an Instance:</label>
<select
name='Select a JSON Schema Validator'
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'
id='Examples'
onChange={handleInstanceChange}
>
{instances.map((instance: any, id: number) => (
<option key={id} value={instance.file}>
{instance.name}
</option>
))}
</select>
</div>
</div>
<div className='overflow-x-auto flex-basis-0 max-w-full min-w-0 shrink lg:max-w-[800px] xl:max-w-[900px]'>
<Highlight
wrapLines={true}
wrapLongLines={true}
customStyle={{
borderRadius: 10,
paddingTop: 15,
paddingBottom: 10,
paddingLeft: 10,
marginBottom: 20,
maxWidth: '100%',
}}
lineNumberStyle={{
marginRight: 10,
}}
style={atomOneDark}
showLineNumbers
startingLineNumber={1}
lineProps={() => {
const isHighlighted = false;
return {
className: `${isHighlighted ? 'bg-code-editor-dark-highlight block ml-10 w-full' : ''} pr-8`,
};
}}
codeTagProps={{
className: 'mr-8',
}}
>
{JSON.stringify(fetchedInstance, null, 2)}
</Highlight>
</div>
<h2 className='text-h5 font-semibold'>Validation Result</h2>
<div className='flex bg-[#282c34] justify-between items-center text-white font-medium flex-row border p-5 rounded-xl'>
<p>{details[0]}</p>

{details[1] ? (
<img src='/icons/green-tick.svg' alt='green tick' />
) : (
<img src='/icons/red-cross.svg' alt='red cross' />
)}
</div>
</div>
</>
);
};

export default GettingStarted;
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ title: Creating your first schema
section: docs
---

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.
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.

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.
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.

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.

<tableofcontent depth={2} />

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

## Validate JSON data against the schema

This section describes how to validate JSON data against the product catalog schema.
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>.

This example JSON data matches the product catalog schema:
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.

```json
{
"productId": 1,
"productName": "An ice sculpture",
"price": 12.50,
"tags": [ "cold", "ice" ],
"dimensions": {
"length": 7.0,
"width": 12.0,
"height": 9.5
},
"warehouseLocation": {
"latitude": -78.75,
"longitude": 20.4
}
}
```
![How JSON Schema works](https://json-schema.org/img/json_schema.svg)

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>.
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.

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.
47 changes: 47 additions & 0 deletions pages/learn/getting-started-step-by-step/index.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import fs from 'fs';

import { getLayout } from '~/components/Sidebar';
import Head from 'next/head';
import { Headline1 } from '~/components/Headlines';
import matter from 'gray-matter';
import StyledMarkdown from '~/components/StyledMarkdown';
import { SectionContext } from '~/context';
import { DocsHelp } from '~/components/DocsHelp';
import GettingStarted from '~/components/GettingStarted';

export async function getStaticProps() {
const block1 = fs.readFileSync(
'pages/learn/getting-started-step-by-step/getting-started-step-by-step.md',
'utf-8',
);
const block2 = fs.readFileSync(
'pages/learn/getting-started-step-by-step/next-steps.md',
'utf-8',
);
const { content: block1Content } = matter(block1);
const { content: block2Content } = matter(block2);
return {
props: {
blocks: [block1Content, block2Content],
},
};
}

export default function StyledValidator({ blocks }: { blocks: any[] }) {
const newTitle = 'Creating your first schema';

return (
<SectionContext.Provider value='docs'>
<Head>
<title>{newTitle}</title>
</Head>
<Headline1>{newTitle}</Headline1>
<StyledMarkdown markdown={blocks[0]} />
<GettingStarted />
<StyledMarkdown markdown={blocks[1]} />
<DocsHelp />
</SectionContext.Provider>
);
}
StyledValidator.getLayout = getLayout;
12 changes: 12 additions & 0 deletions pages/learn/getting-started-step-by-step/next-steps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Getting Started Next Steps
section: docs
---

## What Next?

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:
* Learn more about JSON Schema by visiting the [reference documentation](../understanding-json-schema).
* Explore the details of the current version of the Spec [2020-12](https://json-schema.org/specification).

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 changes: 38 additions & 0 deletions public/data/getting-started-examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"name": "Getting started basic schema",
"default": true,
"file": "/data/getting-started-examples/schemas/default.json",
"instances": [
{
"name": "Valid instance",
"default": true,
"valid": true,
"file": "/data/getting-started-examples/instances/default-ok.json",
"details": "This is a valid JSON instance for the provided JSON Schema"
},
{
"name": "Invalid instance",
"default": false,
"valid": false,
"file": "/data/getting-started-examples/instances/default-ko.json",
"details": "Invalid: The value of 'price' property must be a number"
}
]
},

{
"name": "Getting started extended schema",
"default": false,
"file": "/data/getting-started-examples/schemas/default-extended.json",
"instances": [
{
"name": "Valid instance",
"default": true,
"valid": true,
"file": "/data/getting-started-examples/instances/default-extended-ok.json",
"details": "This is a valid JSON instance for the provided JSON Schema"
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"productId": 1,
"productName": "An ice sculpture",
"price": 12.5,
"tags": ["cold", "ice"],
"dimensions": {
"length": 7.0,
"width": 12.0,
"height": 9.5
},
"warehouseLocation": {
"latitude": -78.75,
"longitude": 20.4
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"productId": 1,
"product": "An ice sculpture",
"price": "100"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"productId": 1,
"productName": "An ice sculpture",
"price": 12.5,
"tags": ["cold", "ice"]
}
Loading
Loading