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

[Docs] Update: DSLSchema #304

Merged
merged 11 commits into from
Mar 1, 2024
105 changes: 105 additions & 0 deletions docs/site/pages/dsl/dsl-schema-type.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
lexfm marked this conversation as resolved.
Show resolved Hide resolved
title: DSLSchema
---

## DSLSchema

A `DSLSchema` type is provided in order be able to define the acceptable data types to be used and the validators to be defined for authoring the DSL data schema in our workspace. In the following example we'll generate everything we need for our DSLSchema type instance for checking based on the data types and validators available in the Player plugin `common-types-plugin`. For this purpose you can use some types and utilities found in the `@player-tools/dsl` package.
lexfm marked this conversation as resolved.
Show resolved Hide resolved

We'll start out with generating the data types object properties and data type references:


```typescript
import {
DSLSchema,
getObjectReferences,
DataTypeReference,
ValidationRefProps
} from '@player-tools/dsl'

import {
dataTypes as coreDataTypes,
validators as coreValidators
} from '@player-ui/common-types-plugin';

/** Grouping the data types into a new object, so we can also combine more type sets in the future */
const coreDataSet = {
...coreDataTypes
}

/** Abstracting the types set into their types */
type CoreDataTypes = typeof coreDataSet

/** Generating the data type property union */
type CoreDataTypeProps = keyof CoreDataTypes

/** Generating the data references types based in their types from above */
type CoreDataTypeRefs = {
/** Property name with DataType object */
[Property in keyof CoreDataTypes as `${Property}Ref`]: {
/** DataType name */
type: Property;
};
};

/** Using getObjectReferences helper to generate the actual data type references */
export const dataRefs = getObjectReferences<CoreDataTypes, CoreDataTypeRefs>(
coreDataSet
);
```

We'll proceed generating the validation object properties and validation function references with similar steps:


```typescript
/** Grouping the validator functions into a new object, so we can also combine more validator sets in the future */
const coreValidatorSet = {
...coreValidators
}

/** Generating the validator function references types based in their functions from above. Each validator ref will include its inherent required and optional own parameters, as well all properties that are part of the ValidationRefProps type*/
type CoreValidatorRefs = {
/** Property name with validator ref object */
[Property in keyof CoreValidatorTypes as `${Property}Ref`]: {
/** Valiator name */
type: Property;
} & Parameters<CoreValidatorTypes[Property]>[2] &
ValidationRefProps;
};

/** We generate the union type from the Validators Refs above*/
type CoreValidatorRefType = CoreValidatorRefs[keyof CoreValidatorRefs];

```
lexfm marked this conversation as resolved.
Show resolved Hide resolved

The final step is to put our generated data type properties union, and our validator function references union as generics for the `DataTypeReference` type which is the sole type we pass into the `DSLSchema` type instance:

```typescript
export type MyWorkspaceDSLSchema = DSLSchema<
DataTypeReference<DataTypeProp, ValidatorRef>
>;
```

Finally this is how to use the schema: By adding the statement `satisfies` followed by your `DSLSchema` generated type, Typescript will show if there is anything not compliant with the data types and validation functions we defined in the schema:
lexfm marked this conversation as resolved.
Show resolved Hide resolved

```typescript
import { MyWorkspaceDSLSchema, dataRefs } from './MyTypes'

const { BooleanTypeRef } = dataRefs

const exampleSchema = {
myDataSet = {
firstPath: BooleanTypeRef
secondPath: {
/** To be able to define validator functions we need to elaborate the data reference object */
lexfm marked this conversation as resolved.
Show resolved Hide resolved
type: 'TextType',
validation: [
{
type: 'required',
message: 'This field is required'
}
]
}
}
} satisfies MyWorkspaceDSLSchema
```
4 changes: 2 additions & 2 deletions docs/site/pages/dsl/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ const stringWithExp = `Some expr: ${myExpression}`; // 'Some expr: @[foo()]@'

### View

Please refer to the [Views](./views) section for a detailed overview of how to write DSL Views
Please refer to the [Views](../dsl/views) section for a detailed overview of how to write DSL Views

### Schema

Please refer to the [Schema](./schema) section for a detailed overview of how to write DSL Schemas
Please refer to the [Schema](../dsl/schema) section for a detailed overview of how to write DSL Schemas

## Compiling DSL Content

Expand Down
6 changes: 5 additions & 1 deletion docs/site/pages/dsl/schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,13 @@ export default {
navigation,
}
```
### DSLSchema

Please refer to the [DSLSchema](../dsl/dsl-schema-type) section to see the detailed process for creating and consuming a DSLSchema type definition.

# DSL Benefit in Schema

## Format

## Ease of Use
## Ease of Use