Skip to content

Commit 947c5e0

Browse files
author
Sojimmy Nea
committed
feat: convert default value spec to form value
Currently, the OpenAPI support default value specification, although it's more intendened to be treated as an optional field and is better handled on the server side. Still, there might be cases where htis could be appropriate to be set as default in the client side, for example, having a country form value set as `Singapore`, since most of our users are from `Singpoare`.
1 parent bcf21cd commit 947c5e0

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

demo/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Run the following from the `/demo` directory:
66

77
```bash
88
npm install
9-
npm run generate:form
10-
ng build form
9+
npm run generate:forms
10+
ng build forms
1111
ng serve
1212
```

demo/projects/forms/src/lib/myApi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ export const addressModelForm = new FormGroup({
4040
Validators.required,
4141
Validators.pattern(/^[\w\@\!\#\%\&\'\*\+\-\/\=\?\`\{\|\}\~\.]+$/),
4242
Validators.email
43-
])
43+
]),
44+
country: new FormControl('Singapore', [Validators.required, Validators.minLength(1)])
4445
});

demo/swagger.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"definitions": {
4343
"AddressModel": {
44-
"required": ["firstName", "lastName", "address", "city", "postalCode", "emailAddress"],
44+
"required": ["firstName", "lastName", "address", "city", "postalCode", "emailAddress", "country"],
4545
"type": "object",
4646
"properties": {
4747
"firstName": {
@@ -84,6 +84,11 @@
8484
"format": "email",
8585
"pattern": "^[\\w\\@\\!\\#\\%\\&\\'\\*\\+\\-\\/\\=\\?\\`\\{\\|\\}\\~\\.]+$",
8686
"type": "string"
87+
},
88+
"country": {
89+
"type": "string",
90+
"minLength": 1,
91+
"default": "Singapore"
8792
}
8893
}
8994
}

demo/swagger.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ definitions:
4848
- city
4949
- postalCode
5050
- emailAddress
51+
- country
5152
type: object
5253
properties:
5354
firstName:
@@ -84,3 +85,7 @@ definitions:
8485
format: email
8586
pattern: '^[\w\@\!\#\%\&\''\*\+\-\/\=\?\`\{\|\}\~\.]+$'
8687
type: string
88+
country:
89+
type: string
90+
minLength: 1
91+
default: 'Singapore',

src/generator-lib.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function makeFieldRules(fieldName: string, definition: Definition): string {
4949
}
5050

5151
function makeField(fieldName: string, definition: Definition): string {
52-
return `"${fieldName}": new FormControl(null, [${makeFieldRules(fieldName, definition)}])`;
52+
const value = 'default' in definition.properties[fieldName] ? `'${definition.properties[fieldName].default}'` : null;
53+
return `"${fieldName}": new FormControl(${value}, [${makeFieldRules(fieldName, definition)}])`;
5354
}
5455

5556
function makeFieldsBody(definition: Definition): string[] {

0 commit comments

Comments
 (0)