Skip to content

Commit

Permalink
Upgrade typescript to 3.0.3, update json schema to draft serafin-labs#7
Browse files Browse the repository at this point in the history
…, handle nullable, improve literal schema typings
  • Loading branch information
sebastien-de-saint-florent committed Sep 19, 2018
1 parent f4a0ed3 commit be63f58
Show file tree
Hide file tree
Showing 11 changed files with 1,185 additions and 839 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/lib/**/*Tests.js"
"${workspaceFolder}/lib/test.js"
],
"internalConsoleOptions": "openOnSessionStart"
},
Expand All @@ -38,4 +38,4 @@
"stopOnEntry": false
}
]
}
}
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ npm i @serafin/schema-builder

## Why Schema Builder?

JSON schema is the base of Open API documents so it's really important for Serafin framework.
JSON schema is the base of Open API so it's really important for Serafin framework.
JSON Schema is powerfull but it is also verbose.

On top of the JSON schema, you also have to create the Typescript interface that it represents. If you take in account other schemas and interfaces you have to define (one for the post body, one for the patch body, one for get query parameters, etc.), it starts to be problematic.
On top of a JSON schema, you also have to create the Typescript interface that it represents. If you take in account other schemas and interfaces you have to define (one for the post body, one for the patch body, one for get query parameters, etc.), it starts to be problematic.

Schema builder is here to save you from all this tedious work!

Expand Down Expand Up @@ -163,7 +163,7 @@ type UserPatch = {
}
```
We can now use the ```validate``` method to validate data against our schema. The validation use ```Ajv``` and a custom meta schema that match Open Api 3 specifications.
We can now use the ```validate``` method to validate data against our schema. The validation use ```Ajv``` with Json Schema draft #7.
```typescript
userPatchSchema.validate({
Expand All @@ -177,13 +177,13 @@ There's more! This was a simple example. This library provides also a lot of tra
## Usage
Since it's a Typescript library, intellisense and code comments provide already a good description of the Api.
Since it's a Typescript library, intellisense and code comments provide already a good description of the methods.
This section will focus on advanced transformation methods and how to use them. Refer to the code for the rest.
### clone
All the methods of ```SchemaBuilder``` may affect the json schema and the generic type. When you need a new version of a schema, you have to clone the original schema first to ensure you don't cause any side effects.
All the methods of ```SchemaBuilder``` may affect the json schema and its generic type. When you need a new version of a schema, you have to clone the original schema first to ensure you don't cause any side effects.
```typescript
let originalSchema = SchemaBuilder.emptySchema()
Expand Down Expand Up @@ -367,11 +367,9 @@ You can override this configuration using the ```configureValidation``` method.

You can also force the validation function to be cached right away ```schema.cacheValidationFunction()``` and/or ```this.cacheListValidationFunction()```

## Experimental features
### Literal Json Schema

```SchemaBuilder``` contains a ```fromJsonSchema``` method that has the ability to deduce the type from the schema parameter directly. The schema has to be provided in a litteral form and use string literals for ```type```, ```enum``` and ```required```.

/!\ This feature is experimental and might still change a lot.
```SchemaBuilder``` contains a ```fromJsonSchema``` method that has the ability to deduce the type from the schema parameter directly. The schema has to be provided in a litteral form and use string literals for ```type```, ```enum```, ```const``` and ```required```.

For example:

Expand Down Expand Up @@ -431,10 +429,6 @@ type T = {
}
```
Only ```oneOf``` combination keyword is supported currently but has some limitations.
If anyone is interested in the super challenge of typing ```allOf``` and ```anyOf```, please contact me :)
## What's next?
```schema-builder``` is a component of **Serafin** framework. You can go to the main documentation to learn more about it : [https://github.com/serafin-labs/serafin](https://github.com/serafin-labs/serafin)
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "2"
services:
app:
image: serafinlabs/nodejs:8-dev
image: serafinlabs/nodejs:10-dev
command: "npm run-script dev"
ports:
- "9229:9229"
Expand Down
Loading

0 comments on commit be63f58

Please sign in to comment.