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: Update refined type for typescript with type guard validation #168

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ Wirespec can read and convert OpenApiSpecification (OAS) files.

Wirespec knows four definitions: `refined`, `enum`, `type`, `endpoint`.

```
### Refined
```wirespec
refined DEFINITION /REGEX/g
```

### Enum
```wirespec
enum DEFINITION {
ENTRY, ENTRY, ...
}
```

### Type
```wirespec
type DEFINITION {
IDENTIFIER: REFERENCE
}
```

### Endpoint
```wirespec
endpoint DEFINITION METHOD [INPUT_REFERENCE] PATH [? QUERY] [# HEADER] -> {
STATUS -> REFERENCE
}

```

## Example
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
refined TodoId /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/g
refined TodoId /^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}$/g

type Todo {
id: TodoId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
refined TodoId /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/g
refined TodoId /^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}$/g

type Todo {
id: TodoId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,15 @@ class TypeScriptEmitter(logger: Logger = noLogger) : AbstractEmitter(logger) {
}

override fun Refined.emit() = withLogging(logger) {
"""export type ${name.sanitizeSymbol()} = {
|${SPACER}value: string
|}
|const validate$name = (type: $name) => (${validator.emit()}).test(type.value);
|
"""export type ${name.sanitizeSymbol()} = string;
|const regExp$name = ${validator.emit()};
|export const validate$name = (value: string): value is ${name.sanitizeSymbol()} =>
|${SPACER}regExp$name.test(value);
|""".trimMargin()
}

override fun Refined.Validator.emit() = withLogging(logger) {
"new RegExp('${value.drop(1).dropLast(1)}')"
"RegExp('${value.drop(1).dropLast(1)}')"
}

override fun Endpoint.emit() = withLogging(logger) {
Expand Down Expand Up @@ -149,7 +148,7 @@ class TypeScriptEmitter(logger: Logger = noLogger) : AbstractEmitter(logger) {
""".trimMargin()
}

override fun Definition.emitName(): String = when(this){
override fun Definition.emitName(): String = when (this) {
is Endpoint -> this.name
is Enum -> this.name
is Refined -> this.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,11 @@ class CompileRefinedTest {
@Test
fun testRefinedTypeScript() {
val ts = """
export type TodoId = {
value: string
}
const validateTodoId = (type: TodoId) => (new RegExp('^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}${'$'}')).test(type.value);


""".trimIndent()
|export type TodoId = string;
|const regExpTodoId = RegExp('^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}${'$'}');
|export const validateTodoId = (value: string): value is TodoId =>
| regExpTodoId.test(value);
|""".trimMargin()

compiler(TypeScriptEmitter(logger = logger)) shouldBeRight ts
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
refined TodoId /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/g
refined TodoId /^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}$/g

type Todo {
id: TodoId,
Expand Down
2 changes: 1 addition & 1 deletion types/endpoint.ws
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
refined TodoIdentifier /^[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12}$/g
refined TodoIdentifier /^[0-9a-f]{8}\\b-[0-9a-f]{4}\\b-[0-9a-f]{4}\\b-[0-9a-f]{4}\\b-[0-9a-f]{12}$/g
refined Name /^[0-9a-zA-Z]{1,50}$/g
refined DutchPostalCode /^([0-9]{4}[A-Z]{2})$/g
refined Date /^([0-9]{2}-[0-9]{2}-20[0-9]{2})$/g
Expand Down
Loading