Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚙️ Allow min/max/number on template types #774

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
rowanc1 marked this conversation as resolved.
Show resolved Hide resolved
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);
fwkoch marked this conversation as resolved.
Show resolved Hide resolved
}
}
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,
});
rowanc1 marked this conversation as resolved.
Show resolved Hide resolved
} 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
Loading