@@ -240,9 +240,9 @@ type standard = {
240
240
validate : 'any 'value . 'any => {"value" : 'value },
241
241
}
242
242
243
- type numberFormat = | @as ("int32" ) Int32
243
+ type numberFormat = | @as ("int32" ) Int32 | @ as ( "port" ) Port
244
244
245
- type internalFormat = numberFormat
245
+ type format = numberFormat
246
246
247
247
@unboxed
248
248
type additionalItemsMode = | @as ("strip" ) Strip | @as ("strict" ) Strict
@@ -403,7 +403,7 @@ and internal = {
403
403
mutable description ?: string ,
404
404
mutable deprecated ?: bool ,
405
405
mutable examples ?: array <unknown >,
406
- format ?: internalFormat ,
406
+ mutable format ?: format ,
407
407
mutable has ?: dict <bool >,
408
408
advanced ?: bool , // TODO: Rename/remove it when have a chance
409
409
mutable anyOf ?: array <internal >,
@@ -434,6 +434,7 @@ and untagged = private {
434
434
tag : tag ,
435
435
const ?: unknown ,
436
436
class ?: unknown ,
437
+ format ?: format ,
437
438
name ?: string ,
438
439
title ?: string ,
439
440
description ?: string ,
@@ -2876,7 +2877,7 @@ module Int = {
2876
2877
type kind =
2877
2878
| Min ({value : int })
2878
2879
| Max ({value : int })
2879
- | Port
2880
+
2880
2881
type t = {
2881
2882
kind : kind ,
2882
2883
message : string ,
@@ -2993,7 +2994,7 @@ let rec to = (from, target) => {
2993
2994
b .code ++
2994
2995
switch format {
2995
2996
| None => ` Number.isNaN(${outputVar})`
2996
- | Some (Int32 ) =>
2997
+ | Some (_ ) =>
2997
2998
` (${b
2998
2999
-> B.refinement(~inputVar= outputVar, ~schema= target, ~negative= true)
2999
3000
-> Js.String2.sliceToEnd(~from= 2)})`
@@ -4611,17 +4612,29 @@ let intMax = (schema, maxValue, ~message as maybeMessage=?) => {
4611
4612
)
4612
4613
}
4613
4614
4614
- let port = (schema , ~message = "Invalid port" ) => {
4615
- schema -> addRefinement (
4616
- ~metadataId = Int .Refinement .metadataId ,
4617
- ~refiner = (b , ~inputVar , ~selfSchema as _ , ~path ) => {
4618
- ` if(${inputVar}<1||${inputVar}>65535){${b-> B.fail(~message, ~path)}}`
4619
- },
4620
- ~refinement = {
4621
- kind : Port ,
4622
- message ,
4623
- },
4624
- )
4615
+ let port = (schema , ~message = ?) => {
4616
+ let mutStandard =
4617
+ schema
4618
+ -> internalRefine ((b , ~inputVar , ~selfSchema , ~path ) => {
4619
+ ` ${inputVar}>0&&${inputVar}<65536&&${inputVar}%1===0||${switch message {
4620
+ | Some(m) = > b-> B.fail(~message= m, ~path)
4621
+ | None = >
4622
+ b-> B.failWithArg(
4623
+ ~path,
4624
+ input = > InvalidType({
4625
+ expected: selfSchema-> fromInternal,
4626
+ received: input,
4627
+ }),
4628
+ inputVar,
4629
+ )
4630
+ }};`
4631
+ })
4632
+ -> toInternal
4633
+
4634
+ mutStandard .format = Some (Port )
4635
+ (mutStandard -> reverse ).format = Some (Port )
4636
+
4637
+ mutStandard -> fromInternal
4625
4638
}
4626
4639
4627
4640
let floatMin = (schema , minValue , ~message as maybeMessage = ?) => {
@@ -5029,11 +5042,15 @@ module RescriptJSONSchema = {
5029
5042
-> Int .refinements
5030
5043
-> Js .Array2 .forEach (refinement => {
5031
5044
switch refinement {
5032
- | {kind : Port } => ()
5033
5045
| {kind : Max ({value })} => jsonSchema .maximum = Some (value -> Js .Int .toFloat )
5034
5046
| {kind : Min ({value })} => jsonSchema .minimum = Some (value -> Js .Int .toFloat )
5035
5047
}
5036
5048
})
5049
+ | Some (Port ) => {
5050
+ jsonSchema .type_ = Some (Arrayable .single (#integer ))
5051
+ jsonSchema .maximum = Some (65535 .)
5052
+ jsonSchema .minimum = Some (0 .)
5053
+ }
5037
5054
| None =>
5038
5055
jsonSchema .type_ = Some (Arrayable .single (#number ))
5039
5056
schema
@@ -5526,7 +5543,7 @@ let min = (schema, minValue, ~message as maybeMessage=?) => {
5526
5543
switch schema {
5527
5544
| String (_ ) => schema -> stringMinLength (minValue , ~message = ?maybeMessage )
5528
5545
| Array (_ ) => schema -> arrayMinLength (minValue , ~message = ?maybeMessage )
5529
- | Number ({format : Int32 }) => schema -> intMin (minValue , ~message = ?maybeMessage )
5546
+ | Number ({format : Int32 | Port }) => schema -> intMin (minValue , ~message = ?maybeMessage )
5530
5547
| Number (_ ) => schema -> floatMin (minValue -> Obj .magic , ~message = ?maybeMessage )
5531
5548
| _ =>
5532
5549
InternalError .panic (
@@ -5539,7 +5556,7 @@ let max = (schema, maxValue, ~message as maybeMessage=?) => {
5539
5556
switch schema {
5540
5557
| String (_ ) => schema -> stringMaxLength (maxValue , ~message = ?maybeMessage )
5541
5558
| Array (_ ) => schema -> arrayMaxLength (maxValue , ~message = ?maybeMessage )
5542
- | Number ({format : Int32 }) => schema -> intMax (maxValue , ~message = ?maybeMessage )
5559
+ | Number ({format : Int32 | Port }) => schema -> intMax (maxValue , ~message = ?maybeMessage )
5543
5560
| Number (_ ) => schema -> floatMax (maxValue -> Obj .magic , ~message = ?maybeMessage )
5544
5561
| _ =>
5545
5562
InternalError .panic (
0 commit comments