Skip to content

Commit

Permalink
⚙️ Allow min/max/number on template types (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Nov 23, 2023
1 parent 81a47ef commit a63bc6a
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-bats-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-templates': patch
---

Allow min/max/integer on number template types
126 changes: 123 additions & 3 deletions packages/myst-templates/src/validators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ describe('validateTemplateOptionDefinition', () => {
description: 'desc',
required: true,
default: 'a',
max_chars: 10,
condition: {
id: 'short_title',
value: 'test',
Expand All @@ -385,12 +384,34 @@ describe('validateTemplateOptionDefinition', () => {
description: 'desc',
required: true,
default: 'a',
max_chars: 10,
condition: {
id: 'short_title',
value: 'test',
},
});
expect(opts.messages.errors?.length ?? 0).toEqual(0);
});
it('object with all properties passes', async () => {
expect(
validateTemplateOptionDefinition(
session,
{
id: 'key',
type: 'string',
description: 'desc',
required: true,
max_chars: 10,
},
opts,
),
).toEqual({
id: 'key',
type: 'string',
description: 'desc',
required: true,
max_chars: 10,
});
expect(opts.messages.errors?.length ?? 0).toEqual(0);
});
it('invalid choices errors', async () => {
expect(
Expand Down Expand Up @@ -466,6 +487,105 @@ describe('validateTemplateOptionDefinition', () => {
});
expect(opts.messages.errors?.length).toEqual(1);
});
it('basic min / max', async () => {
expect(
validateTemplateOptionDefinition(
session,
{
id: 'key',
type: 'number',
min: 1,
max: 5,
integer: true,
},
opts,
),
).toEqual({
id: 'key',
type: 'number',
min: 1,
max: 5,
integer: true,
});
expect(opts.messages.errors?.length ?? 0).toEqual(0);
});
it('basic min / max with bad default errors', async () => {
expect(
validateTemplateOptionDefinition(
session,
{
id: 'key',
type: 'number',
min: 1,
max: 5,
integer: true,
default: 6,
},
opts,
),
).toEqual({
id: 'key',
type: 'number',
min: 1,
max: 5,
integer: true,
});
expect(opts.messages.errors?.length).toEqual(1);
});
it('min / max flipped', async () => {
expect(
validateTemplateOptionDefinition(
session,
{
id: 'key',
type: 'number',
min: 5,
max: 1,
},
opts,
),
).toEqual({
id: 'key',
type: 'number',
min: 1,
max: 5,
});
expect(opts.messages.warnings?.length).toEqual(1);
});
it('cannot use choices when not a choice', async () => {
expect(
validateTemplateOptionDefinition(
session,
{
id: 'key',
type: 'number',
choices: ['a', 'b'],
},
opts,
),
).toEqual({
id: 'key',
type: 'number',
});
expect(opts.messages.errors?.length).toEqual(1);
});
it('cannot use integer on a string type', async () => {
expect(
validateTemplateOptionDefinition(
session,
{
id: 'key',
type: 'string',
integer: true,
},
opts,
),
).toEqual({
id: 'key',
type: 'string',
});
expect(opts.messages.errors?.length).toEqual(1);
});
it('condition with id passes', async () => {
expect(
validateTemplateOptionDefinition(
Expand Down Expand Up @@ -644,7 +764,7 @@ describe('validateTemplatePartDefinition', () => {
});
expect(opts.messages.errors?.length).toEqual(1);
});
it('valid part definiton passes', async () => {
it('valid part definition passes', async () => {
expect(
validateTemplatePartDefinition(
{
Expand Down
65 changes: 57 additions & 8 deletions packages/myst-templates/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
validateString,
validateUrl,
validationError,
validationWarning,
} from 'simple-validators';
import type { ValidationOptions } from 'simple-validators';
import type {
Expand Down Expand Up @@ -268,6 +269,9 @@ export function validateTemplateOptionDefinition(
'default',
'required',
'choices',
'min',
'max',
'integer',
'max_chars',
'condition',
],
Expand Down Expand Up @@ -307,16 +311,61 @@ export function validateTemplateOptionDefinition(
output.required = validateBoolean(value.required, incrementOptions('required', opts));
}
if (defined(value.choices)) {
output.choices = validateList(value.choices, incrementOptions('choices', opts), (val, ind) => {
return validateString(val, incrementOptions(`choices.${ind}`, opts));
});
if (output.type === 'choice') {
output.choices = validateList(
value.choices,
incrementOptions('choices', opts),
(val, ind) => {
return validateString(val, incrementOptions(`choices.${ind}`, opts));
},
);
} else {
validationError('type must be "choice" to use "choices" option', opts);
}
}
if (defined(value.max_chars)) {
output.max_chars = validateNumber(value.max_chars, {
min: 0,
integer: true,
...incrementOptions('max_chars', opts),
});
if (output.type === 'string') {
output.max_chars = validateNumber(value.max_chars, {
min: 0,
integer: true,
...incrementOptions('max_chars', opts),
});
} else {
validationError('type must be "string" to use "max_chars" option', opts);
}
}
if (defined(value.integer)) {
if (output.type === 'number') {
output.integer = validateBoolean(value.integer, incrementOptions('integer', opts));
} else {
validationError('type must be "number" to use "integer" option', opts);
}
}
if (defined(value.min)) {
if (output.type === 'number') {
output.min = validateNumber(value.min, {
...incrementOptions('min', opts),
integer: output.integer,
});
} else {
validationError('type must be "number" to use "min" option', opts);
}
}
if (defined(value.max)) {
if (output.type === 'number') {
output.max = validateNumber(value.max, {
...incrementOptions('max', opts),
integer: output.integer,
});
} else {
validationError('type must be "number" to use "max" option', opts);
}
}
if (defined(output.min) && defined(output.max) && output.max < output.min) {
validationWarning('"min" and "max" options are flipped', opts);
const [min, max] = [output.min, output.max];
output.min = max;
output.max = min;
}
if (defined(value.condition)) {
output.condition = validateCondition(value.condition, incrementOptions('condition', opts));
Expand Down

0 comments on commit a63bc6a

Please sign in to comment.