Skip to content

Commit

Permalink
Add default value constants to schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
walsha2 committed Aug 6, 2023
1 parent 23c8897 commit aaa49b2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.4.6

* Add default value for schema validation constants

## 0.4.5

* Convert union types to sealed classes
Expand Down
1 change: 1 addition & 0 deletions lib/src/generators/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class SchemaGenerator extends BaseGenerator {
validation = SchemaValidation.numeric(
name: name,
nullable: nullable,
defaultValue: p.defaultValue,
minimum: p.minimum,
maximum: p.maximum,
exclusiveMinimum: p.exclusiveMinimum,
Expand Down
13 changes: 12 additions & 1 deletion lib/src/generators/validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SchemaValidation {
SchemaValidation._({
required this.name,
required this.type,
this.defaultValue,
this.minimum,
this.maximum,
this.minLength,
Expand All @@ -33,6 +34,7 @@ class SchemaValidation {

factory SchemaValidation.numeric({
required String name,
num? defaultValue,
num? minimum,
num? maximum,
num? multipleOf,
Expand All @@ -43,6 +45,7 @@ class SchemaValidation {
return SchemaValidation._(
name: name,
type: SchemaValidationType.numeric,
defaultValue: defaultValue,
minimum: minimum,
maximum: maximum,
multipleOf: multipleOf,
Expand Down Expand Up @@ -85,6 +88,9 @@ class SchemaValidation {
/// The type of value being validated
SchemaValidationType type;

/// Default value
num? defaultValue;

/// Minimum value
num? minimum;

Expand Down Expand Up @@ -119,10 +125,15 @@ class SchemaValidation {
// METHOD: _defineValidations
// ------------------------------------------

_defineValidations() {
void _defineValidations() {
if (type == SchemaValidationType.numeric) {
final nullName = nullable ? '$name != null && $name!' : name;

if (defaultValue != null) {
final conName = '${name}_default_value'.camelCase;
constants[conName] = defaultValue!;
}

if (minimum != null) {
final operator = exclusiveMinimum ?? false ? '<=' : '<';
final conName = '${name}_min_value'.camelCase;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: openapi_spec
description: OpenAPI Specification generator using native Dart code, as well as an all-in-one parser of existing specifications.
version: 0.4.5
version: 0.4.6
maintainer: Taza Technology LLC
repository: https://github.com/tazatechnology/openapi_spec
issue_tracker: https://github.com/tazatechnology/openapi_spec/issues
Expand Down

0 comments on commit aaa49b2

Please sign in to comment.