Skip to content

Commit dc946e8

Browse files
rollpeterdesmet
andauthored
Added "Field Constraints" examples (#1056)
Co-authored-by: Peter Desmet <[email protected]>
1 parent 2ecb9fd commit dc946e8

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed

content/docs/standard/table-schema.mdx

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,76 +647,323 @@ A constraints descriptor `MUST` be a JSON `object` and `MAY` contain one or more
647647

648648
Indicates whether this field cannot be `null`. If required is `false` (the default), then `null` is allowed. See the section on `missingValues` for how, in the physical representation of the data, strings can represent `null` values.
649649

650+
For example, this data file:
651+
652+
```csv
653+
id,name
654+
1,apple
655+
2,
656+
```
657+
658+
With this schema definition:
659+
660+
```json
661+
{
662+
"fields": [
663+
{ "name": "id", "type": "integer" },
664+
{ "name": "name", "type": "string", "constraints": { "required": true } }
665+
]
666+
}
667+
```
668+
669+
Will be invalid because the `name` field is missing a value for the second row.
670+
650671
### `unique`
651672

652673
- **Type**: boolean
653674
- **Fields**: all
654675

655676
If `true`, then all values for that field `MUST` be unique within the data file in which it is found.
656677

678+
For example, this data file:
679+
680+
```csv
681+
id,name
682+
1,apple
683+
2,apple
684+
```
685+
686+
With this schema definition:
687+
688+
```json
689+
{
690+
"fields": [
691+
{ "name": "id", "type": "integer" },
692+
{ "name": "name", "type": "string", "constraints": { "unique": true } }
693+
]
694+
}
695+
```
696+
697+
Will be invalid because the `name` field has a duplicate value.
698+
657699
### `minLength` {#minLength}
658700

659701
- **Type**: integer
660702
- **Fields**: collections (string, array, object)
661703

662704
An integer that specifies the minimum length of a value.
663705

706+
For example, this data file:
707+
708+
```csv
709+
id,name
710+
1,apple
711+
2,plum
712+
```
713+
714+
With this schema definition:
715+
716+
```json
717+
{
718+
"fields": [
719+
{ "name": "id", "type": "integer" },
720+
{ "name": "name", "type": "string", "constraints": { "minLength": 5 } }
721+
]
722+
}
723+
```
724+
725+
Will be invalid because the `name` field has a value that is too short.
726+
664727
### `maxLength` {#maxLength}
665728

666729
- **Type**: integer
667730
- **Fields**: collections (string, array, object)
668731

669732
An integer that specifies the maximum length of a value.
670733

734+
For example, this data file:
735+
736+
```csv
737+
id,name
738+
1,apple
739+
2,grapefruit
740+
```
741+
742+
With this schema definition:
743+
744+
```json
745+
{
746+
"fields": [
747+
{ "name": "id", "type": "integer" },
748+
{ "name": "name", "type": "string", "constraints": { "maxLength": 5 } }
749+
]
750+
}
751+
```
752+
753+
Will be invalid because the `name` field has a value that is too long.
754+
671755
### `minimum`
672756

673757
- **Type**: integer, number, date, time, datetime, duration, year, yearmonth
674758
- **Fields**: integer, number, date, time, datetime, duration, year, yearmonth
675759

676760
Specifies a minimum value for a field. This is different to `minLength` which checks the number of items in the value. A `minimum` value constraint checks whether a field value is greater than or equal to the specified value. The range checking depends on the `type` of the field. E.g. an integer field may have a minimum value of 100; a date field might have a minimum date. If a `minimum` value constraint is specified then the field descriptor `MUST` contain a `type` key.
677761

762+
For example, this data file:
763+
764+
```csv
765+
id,name,price
766+
1,apple,100
767+
2,orange,50
768+
```
769+
770+
With this schema definition:
771+
772+
```json
773+
{
774+
"fields": [
775+
{ "name": "id", "type": "integer" },
776+
{ "name": "name", "type": "string" },
777+
{ "name": "price", "type": "integer", "constraints": { "minimum": 100 } }
778+
]
779+
}
780+
```
781+
782+
Will be invalid because the `price` field has a value that is too low.
783+
678784
### `maximum`
679785

680786
- **Type**: integer, number, date, time, datetime, duration, year, yearmonth
681787
- **Fields**: integer, number, date, time, datetime, duration, year, yearmonth
682788

683789
As for `minimum`, but specifies a maximum value for a field.
684790

791+
For example, this data file:
792+
793+
```csv
794+
id,name,price
795+
1,apple,100
796+
2,orange,150
797+
```
798+
799+
With this schema definition:
800+
801+
```json
802+
{
803+
"fields": [
804+
{ "name": "id", "type": "integer" },
805+
{ "name": "name", "type": "string" },
806+
{ "name": "price", "type": "integer", "constraints": { "maximum": 100 } }
807+
]
808+
}
809+
```
810+
811+
Will be invalid because the `price` field has a value that is too high.
812+
685813
### `exclusiveMinimum` {#exclusiveMinimum}
686814

687815
- **Type**: integer, number, date, time, datetime, duration, year, yearmonth
688816
- **Fields**: integer, number, date, time, datetime, duration, year, yearmonth
689817

690818
As for `minimum`, but for expressing exclusive range.
691819

820+
For example, this data file:
821+
822+
```csv
823+
id,name,price
824+
1,apple,100
825+
2,orange,0
826+
```
827+
828+
With this schema definition:
829+
830+
```json
831+
{
832+
"fields": [
833+
{ "name": "id", "type": "integer" },
834+
{ "name": "name", "type": "string" },
835+
{ "name": "price", "type": "integer", "constraints": { "exclusiveMinimum": 0 } }
836+
]
837+
}
838+
```
839+
840+
Will be invalid because the `price` field has a value that is too low.
841+
692842
### `exclusiveMaximum` {#exclusiveMaximum}
693843

694844
- **Type**: integer, number, date, time, datetime, duration, year, yearmonth
695845
- **Fields**: integer, number, date, time, datetime, duration, year, yearmonth
696846

697847
As for `maximum`, but for expressing exclusive range.
698848

849+
For example, this data file:
850+
851+
```csv
852+
id,name,price
853+
1,apple,100
854+
2,orange,150
855+
```
856+
857+
With this schema definition:
858+
859+
```json
860+
{
861+
"fields": [
862+
{ "name": "id", "type": "integer" },
863+
{ "name": "name", "type": "string" },
864+
{ "name": "price", "type": "integer", "constraints": { "exclusiveMaximum": 150 } }
865+
]
866+
}
867+
```
868+
869+
Will be invalid because the `price` field has a value that is too high.
870+
699871
### `jsonSchema` {#jsonSchema}
700872

701873
- **Type**: object
702874
- **Fields**: array, object
703875

704876
A valid JSON Schema object to validate field values. If a field value conforms to the provided JSON Schema then this field value is valid.
705877

878+
For example, this data file:
879+
880+
```csv
881+
id,name,price
882+
1,apple,{"value": 100}
883+
2,orange,{"value": "bad"}
884+
```
885+
886+
With this schema definition:
887+
888+
```json
889+
{
890+
"fields": [
891+
{ "name": "id", "type": "integer" },
892+
{ "name": "name", "type": "string" },
893+
{
894+
"name": "price",
895+
"type": "object",
896+
"constraints": {
897+
"jsonSchema": {
898+
"type": "object",
899+
"properties": {
900+
"value": { "type": "integer" }
901+
}
902+
}
903+
}
904+
}
905+
]
906+
}
907+
```
908+
909+
Will be invalid because the `price` field has a value that is not an integer.
910+
706911
### `pattern`
707912

708913
- **Type**: string
709914
- **Fields**: string
710915

711916
A regular expression that can be used to test field values. If the regular expression matches then the value is valid. The values of this field `MUST` conform to the standard [XML Schema regular expression syntax](http://www.w3.org/TR/xmlschema-2/#regexs).
712917

918+
For example, this data file:
919+
920+
```csv
921+
id,name
922+
1,apple
923+
2,orange
924+
```
925+
926+
With this schema definition:
927+
928+
```json
929+
{
930+
"fields": [
931+
{ "name": "id", "type": "integer" },
932+
{ "name": "name", "type": "string", "constraints": { "pattern": "^a.*$" } }
933+
]
934+
}
935+
```
936+
937+
Will be invalid because the `name` field has a value that does not match the pattern.
938+
713939
### `enum`
714940

715941
- **Type**: array
716942
- **Fields**: all
717943

718944
The value of the field `MUST` exactly match one of the values in the `enum` array.
719945

946+
For example, this data file:
947+
948+
```csv
949+
id,name
950+
1,apple
951+
2,orange
952+
```
953+
954+
With this schema definition:
955+
956+
```json
957+
{
958+
"fields": [
959+
{ "name": "id", "type": "integer" },
960+
{ "name": "name", "type": "string", "constraints": { "enum": ["apple"] } }
961+
]
962+
}
963+
```
964+
965+
Will be invalid because the `name` field has a value that is not in the `enum` array.
966+
720967
:::note[Implementation Note]
721968

722969
- Implementations `SHOULD` report an error if an attempt is made to evaluate a value against an unsupported constraint.

0 commit comments

Comments
 (0)