-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
842 additions
and
917 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
typed/src/commonMain/kotlin/io/github/nomisrev/openapi/Constraints.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.nomisrev.openapi | ||
|
||
data class NumberConstraint( | ||
val exclusiveMinimum: Boolean, | ||
val minimum: Double, | ||
val exclusiveMaximum: Boolean, | ||
val maximum: Double, | ||
val multipleOf: Double? | ||
) { | ||
constructor(schema: Schema) : this( | ||
schema.exclusiveMinimum ?: false, | ||
schema.minimum ?: Double.NEGATIVE_INFINITY, | ||
schema.exclusiveMaximum ?: false, | ||
schema.maximum ?: Double.POSITIVE_INFINITY, | ||
schema.multipleOf | ||
) | ||
} | ||
|
||
data class TextConstraint(val maxLength: Int, val minLength: Int, val pattern: String?) { | ||
constructor(schema: Schema) : this(schema.maxLength ?: Int.MAX_VALUE, schema.minLength ?: 0, schema.pattern) | ||
} | ||
|
||
data class CollectionConstraint( | ||
val minItems: Int, | ||
val maxItems: Int, | ||
) { | ||
constructor(schema: Schema) : this( | ||
schema.minItems ?: 0, | ||
schema.maxItems ?: Int.MAX_VALUE, | ||
) | ||
} | ||
|
||
// TODO `not` is not supported yet | ||
data class ObjectConstraint( | ||
val minProperties: Int, | ||
val maxProperties: Int, | ||
) { | ||
constructor(schema: Schema): this( | ||
schema.minProperties ?: 0, | ||
schema.maxProperties ?: Int.MAX_VALUE, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
typed/src/commonTest/kotlin/io/github/nomisrev/openapi/ConstraintTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.github.nomisrev.openapi | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ConstraintTest { | ||
@Test | ||
fun intRange() { | ||
assertEquals( | ||
Model.Primitive.int( | ||
constraint = NumberConstraint( | ||
minimum = 1.0, | ||
maximum = 10.0, | ||
exclusiveMinimum = false, | ||
exclusiveMaximum = false, | ||
multipleOf = null | ||
) | ||
), | ||
Schema( | ||
type = Schema.Type.Basic.Integer, | ||
minimum = 1.0, | ||
maximum = 10.0, | ||
exclusiveMinimum = false, | ||
exclusiveMaximum = false | ||
).toModel("IntRange") | ||
) | ||
} | ||
|
||
@Test | ||
fun doubleRange() { | ||
assertEquals( | ||
Model.Primitive.double( | ||
constraint = NumberConstraint( | ||
minimum = 1.0, | ||
maximum = 10.0, | ||
exclusiveMinimum = false, | ||
exclusiveMaximum = false, | ||
multipleOf = null | ||
) | ||
), | ||
Schema( | ||
type = Schema.Type.Basic.Number, | ||
minimum = 1.0, | ||
maximum = 10.0, | ||
exclusiveMinimum = false, | ||
exclusiveMaximum = false | ||
).toModel("IntRange") | ||
) | ||
} | ||
|
||
@Test | ||
fun text() { | ||
assertEquals( | ||
Model.Primitive.string( | ||
constraint = TextConstraint( | ||
maxLength = 10, | ||
minLength = 1, | ||
pattern = null | ||
) | ||
), | ||
Schema( | ||
type = Schema.Type.Basic.String, | ||
maxLength = 10, | ||
minLength = 1, | ||
pattern = null | ||
).toModel("Text") | ||
) | ||
} | ||
|
||
@Test | ||
fun list() { | ||
assertEquals( | ||
Model.Collection.list( | ||
inner = Model.Primitive.string(), | ||
constraint = CollectionConstraint( | ||
minItems = 1, | ||
maxItems = 10 | ||
) | ||
), | ||
Schema( | ||
type = Schema.Type.Basic.Array, | ||
items = ReferenceOr.value(Schema(type = Schema.Type.Basic.String)), | ||
maxItems = 10, | ||
minItems = 1 | ||
).toModel("List") | ||
) | ||
} | ||
|
||
@Test | ||
fun set() { | ||
assertEquals( | ||
Model.Collection.set( | ||
inner = Model.Primitive.string(), | ||
constraint = CollectionConstraint( | ||
minItems = 1, | ||
maxItems = 10 | ||
) | ||
), | ||
Schema( | ||
type = Schema.Type.Basic.Array, | ||
items = ReferenceOr.value(Schema(type = Schema.Type.Basic.String)), | ||
maxItems = 10, | ||
minItems = 1, | ||
uniqueItems = true | ||
).toModel("List") | ||
) | ||
} | ||
|
||
@Test | ||
fun obj() { | ||
assertEquals( | ||
Model.obj( | ||
context = NamingContext.Named("Obj"), | ||
properties = listOf(Model.Object.property("name", Model.Primitive.string())), | ||
inline = listOf(Model.Primitive.string()) | ||
), | ||
Schema( | ||
type = Schema.Type.Basic.Object, | ||
properties = mapOf("name" to ReferenceOr.value(Schema(type = Schema.Type.Basic.String))) | ||
).toModel("Obj") | ||
) | ||
} | ||
} |
Oops, something went wrong.