Skip to content

Support Ranges & Integer Validation In Config Schemas #10117

Open
@solonovamax

Description

@solonovamax

🙋 feature request

Add support config schemas with ranges & restrictions to only integers for config schemas.

🤔 Expected Behavior

The feature would allow specifying optional number properties min and max values for a schema entity of type number for restricting the range, as well as an optional boolean property integer to indicate if it must be an integer (the default, undefined case is the same as false).

😯 Current Behavior

Currently, the config schema only supports declaring a schema with a predefined set of numbers, via the enum property:

export type SchemaNumber = {|
type: 'number',
enum?: Array<number>,
__type?: string,
|};

However, this property is not that useful, as all the possible values must be listed out manually.

💁 Possible Solution

I'm going to open a PR in a few which adds support, because it's not that many lines of change.

🔦 Context

An example usecase is the @parcel/transformer-image plugin:

const JPEG_OUTPUT_SCHEMA: SchemaEntity = {
type: 'object',
properties: {
quality: {
type: 'number',
},
progressive: {
type: 'boolean',
},
chromaSubsampling: {
type: 'string',
},
optimiseCoding: {
type: 'boolean',
},
optimizeCoding: {
type: 'boolean',
},
mozjpeg: {
type: 'boolean',
},
trellisQuantisation: {
type: 'boolean',
},
overshootDeringing: {
type: 'boolean',
},
optimiseScans: {
type: 'boolean',
},
optimizeScans: {
type: 'boolean',
},
quantisationTable: {
type: 'number',
},
quantizationTable: {
type: 'number',
},
force: {
type: 'boolean',
},
},
additionalProperties: true,
};

For many of these properties, there are range restrictions on them. However, there are too many values to realistically list all of them. Take quality for example, it must be in the range of 1-100, however listing all integers from 1 to 100 would be rather tedious.
So, it would be much better if instead it could be specified as the following:

properties: {
  quality: {
    type: 'number',
    min: 1,
    max: 100,
    integer: true,
  },
  ...
}

This would restrict it to only integers in the range of 1-100.

💻 Examples

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions