@prairielearn/zod 1.1.4 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +37 -1
- package/dist/index.test.js.map +1 -1
- package/package.json +3 -3
- package/src/index.test.ts +50 -1
- package/src/index.ts +26 -0
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -73,3 +73,14 @@ export declare const IntervalSchema: z.ZodEffects<z.ZodUnion<[z.ZodString, z.Zod
|
|
|
73
73
|
* Useful for parsing dates from JSON, which are always strings.
|
|
74
74
|
*/
|
|
75
75
|
export declare const DateFromISOString: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodDate]>, string | Date, string | Date>, Date, string | Date>;
|
|
76
|
+
/**
|
|
77
|
+
* A Zod schema that coerces a non-empty string to an integer or an empty string to null.
|
|
78
|
+
* This is useful for form number inputs that are not required but we do not want to
|
|
79
|
+
* use an empty string to compute values.
|
|
80
|
+
*/
|
|
81
|
+
export declare const IntegerFromStringOrEmptySchema: z.ZodEffects<z.ZodUnion<[z.ZodNull, z.ZodNumber]>, number | null, unknown>;
|
|
82
|
+
/**
|
|
83
|
+
* A Zod schema for an arrray of string values from either a string or an array of
|
|
84
|
+
* strings.
|
|
85
|
+
*/
|
|
86
|
+
export declare const ArrayFromStringOrArraySchema: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>, string[], string | string[]>;
|
package/dist/index.js
CHANGED
|
@@ -90,4 +90,27 @@ export const DateFromISOString = z
|
|
|
90
90
|
message: 'must be a valid ISO date string',
|
|
91
91
|
})
|
|
92
92
|
.transform((s) => new Date(s));
|
|
93
|
+
/**
|
|
94
|
+
* A Zod schema that coerces a non-empty string to an integer or an empty string to null.
|
|
95
|
+
* This is useful for form number inputs that are not required but we do not want to
|
|
96
|
+
* use an empty string to compute values.
|
|
97
|
+
*/
|
|
98
|
+
export const IntegerFromStringOrEmptySchema = z.preprocess((value) => (value === '' ? null : value), z.union([z.null(), z.coerce.number().int()]));
|
|
99
|
+
/**
|
|
100
|
+
* A Zod schema for an arrray of string values from either a string or an array of
|
|
101
|
+
* strings.
|
|
102
|
+
*/
|
|
103
|
+
export const ArrayFromStringOrArraySchema = z
|
|
104
|
+
.union([z.string(), z.array(z.string())])
|
|
105
|
+
.transform((s) => {
|
|
106
|
+
if (s === null) {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
else if (Array.isArray(s)) {
|
|
110
|
+
return s;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
return [s];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
93
116
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,sBAAsB,GAAG,IAAI,CAAC;AACpC,MAAM,sBAAsB,GAAG,EAAE,GAAG,sBAAsB,CAAC;AAC3D,MAAM,oBAAoB,GAAG,EAAE,GAAG,sBAAsB,CAAC;AACzD,MAAM,mBAAmB,GAAG,EAAE,GAAG,oBAAoB,CAAC;AACtD,MAAM,qBAAqB,GAAG,EAAE,GAAG,mBAAmB,CAAC;AACvD,MAAM,oBAAoB,GAAG,MAAM,GAAG,mBAAmB,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC;KACvC,MAAM,EAAE;KACR,QAAQ,EAAE;KACV,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEzB;;;;;;;;;;GAUG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BAAoB;QACrC,QAAQ,CAAC,MAAM,GAAG,qBAAqB;QACvC,QAAQ,CAAC,IAAI,GAAG,mBAAmB;QACnC,QAAQ,CAAC,KAAK,GAAG,oBAAoB;QACrC,QAAQ,CAAC,OAAO,GAAG,sBAAsB;QACzC,QAAQ,CAAC,OAAO,GAAG,sBAAsB;QACzC,QAAQ,CAAC,YAAY,CACtB,CAAC;AACJ,CAAC,CAAC,CAAC;AAEL;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC;KAC/B,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;KAC7B,MAAM,CACL,CAAC,CAAC,EAAE,EAAE;IACJ,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;IACzB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;AACvC,CAAC,EACD;IACE,OAAO,EAAE,iCAAiC;CAC3C,CACF;KACA,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC","sourcesContent":["import parsePostgresInterval from 'postgres-interval';\nimport { z } from 'zod';\n\nconst INTERVAL_MS_PER_SECOND = 1000;\nconst INTERVAL_MS_PER_MINUTE = 60 * INTERVAL_MS_PER_SECOND;\nconst INTERVAL_MS_PER_HOUR = 60 * INTERVAL_MS_PER_MINUTE;\nconst INTERVAL_MS_PER_DAY = 24 * INTERVAL_MS_PER_HOUR;\nconst INTERVAL_MS_PER_MONTH = 30 * INTERVAL_MS_PER_DAY;\nconst INTERVAL_MS_PER_YEAR = 365.25 * INTERVAL_MS_PER_DAY;\n\n/**\n * A Zod schema for a boolean from a single checkbox input in the body\n * parameters from a form. This will return a boolean with a value of `true` if\n * the checkbox is checked (the input is present) and `false` if it is not\n * checked.\n */\nexport const BooleanFromCheckboxSchema = z\n .string()\n .optional()\n .transform((s) => !!s);\n\n/**\n * A Zod schema for a PostgreSQL ID.\n *\n * We store IDs as BIGINT in PostgreSQL, which are passed to JavaScript as\n * either strings (if the ID is fetched directly) or numbers (if passed via\n * `to_jsonb()`). This schema coerces the ID to a string to ensure consistent\n * handling.\n *\n * The `refine` step is important to ensure that the thing we've coerced to a\n * string is actually a number. If it's not, we want to fail quickly.\n */\nexport const IdSchema = z\n .string({ coerce: true })\n .refine((val) => /^\\d+$/.test(val), { message: 'ID is not a non-negative integer' });\n\n/**\n * A Zod schema for the objects produced by the `postgres-interval` library.\n */\nconst PostgresIntervalSchema = z.object({\n years: z.number().default(0),\n months: z.number().default(0),\n days: z.number().default(0),\n hours: z.number().default(0),\n minutes: z.number().default(0),\n seconds: z.number().default(0),\n milliseconds: z.number().default(0),\n});\n\n/**\n * A Zod schema for a PostgreSQL interval.\n *\n * This handles two representations of an interval:\n *\n * - A string like \"1 year 2 days\", which is how intervals will be represented\n * if they go through `to_jsonb` in a query.\n * - A {@link PostgresIntervalSchema} object, which is what we'll get if a\n * query directly returns an interval column. The interval will already be\n * parsed by `postgres-interval` by way of `pg-types`.\n *\n * In either case, we convert the interval to a number of milliseconds.\n */\nexport const IntervalSchema = z\n .union([z.string(), PostgresIntervalSchema])\n .transform((interval) => {\n if (typeof interval === 'string') {\n interval = parsePostgresInterval(interval);\n }\n\n // This calculation matches Postgres's behavior when computing the number of\n // milliseconds in an interval with `EXTRACT(epoch from '...'::interval) * 1000`.\n // The noteworthy parts of this conversion are that 1 year = 365.25 days and\n // 1 month = 30 days.\n return (\n interval.years * INTERVAL_MS_PER_YEAR +\n interval.months * INTERVAL_MS_PER_MONTH +\n interval.days * INTERVAL_MS_PER_DAY +\n interval.hours * INTERVAL_MS_PER_HOUR +\n interval.minutes * INTERVAL_MS_PER_MINUTE +\n interval.seconds * INTERVAL_MS_PER_SECOND +\n interval.milliseconds\n );\n });\n\n/**\n * A Zod schema for a date string in ISO format.\n *\n * Accepts either a string or a Date object. If a string is passed, it is\n * validated and parsed as an ISO date string.\n *\n * Useful for parsing dates from JSON, which are always strings.\n */\nexport const DateFromISOString = z\n .union([z.string(), z.date()])\n .refine(\n (s) => {\n const date = new Date(s);\n return !Number.isNaN(date.getTime());\n },\n {\n message: 'must be a valid ISO date string',\n },\n )\n .transform((s) => new Date(s));\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,sBAAsB,GAAG,IAAI,CAAC;AACpC,MAAM,sBAAsB,GAAG,EAAE,GAAG,sBAAsB,CAAC;AAC3D,MAAM,oBAAoB,GAAG,EAAE,GAAG,sBAAsB,CAAC;AACzD,MAAM,mBAAmB,GAAG,EAAE,GAAG,oBAAoB,CAAC;AACtD,MAAM,qBAAqB,GAAG,EAAE,GAAG,mBAAmB,CAAC;AACvD,MAAM,oBAAoB,GAAG,MAAM,GAAG,mBAAmB,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC;KACvC,MAAM,EAAE;KACR,QAAQ,EAAE;KACV,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEzB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC;KACtB,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;KACxB,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,EAAE,kCAAkC,EAAE,CAAC,CAAC;AAEvF;;GAEG;AACH,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC;IACtC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,C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{ z } from 'zod';\n\nconst INTERVAL_MS_PER_SECOND = 1000;\nconst INTERVAL_MS_PER_MINUTE = 60 * INTERVAL_MS_PER_SECOND;\nconst INTERVAL_MS_PER_HOUR = 60 * INTERVAL_MS_PER_MINUTE;\nconst INTERVAL_MS_PER_DAY = 24 * INTERVAL_MS_PER_HOUR;\nconst INTERVAL_MS_PER_MONTH = 30 * INTERVAL_MS_PER_DAY;\nconst INTERVAL_MS_PER_YEAR = 365.25 * INTERVAL_MS_PER_DAY;\n\n/**\n * A Zod schema for a boolean from a single checkbox input in the body\n * parameters from a form. This will return a boolean with a value of `true` if\n * the checkbox is checked (the input is present) and `false` if it is not\n * checked.\n */\nexport const BooleanFromCheckboxSchema = z\n .string()\n .optional()\n .transform((s) => !!s);\n\n/**\n * A Zod schema for a PostgreSQL ID.\n *\n * We store IDs as BIGINT in PostgreSQL, which are passed to JavaScript as\n * either strings (if the ID is fetched directly) or numbers (if passed via\n * `to_jsonb()`). This schema coerces the ID to a string to ensure consistent\n * handling.\n *\n * The `refine` step is important to ensure that the thing we've coerced to a\n * string is actually a number. If it's not, we want to fail quickly.\n */\nexport const IdSchema = z\n .string({ coerce: true })\n .refine((val) => /^\\d+$/.test(val), { message: 'ID is not a non-negative integer' });\n\n/**\n * A Zod schema for the objects produced by the `postgres-interval` library.\n */\nconst PostgresIntervalSchema = z.object({\n years: z.number().default(0),\n months: z.number().default(0),\n days: z.number().default(0),\n hours: z.number().default(0),\n minutes: z.number().default(0),\n seconds: z.number().default(0),\n milliseconds: z.number().default(0),\n});\n\n/**\n * A Zod schema for a PostgreSQL interval.\n *\n * This handles two representations of an interval:\n *\n * - A string like \"1 year 2 days\", which is how intervals will be represented\n * if they go through `to_jsonb` in a query.\n * - A {@link PostgresIntervalSchema} object, which is what we'll get if a\n * query directly returns an interval column. The interval will already be\n * parsed by `postgres-interval` by way of `pg-types`.\n *\n * In either case, we convert the interval to a number of milliseconds.\n */\nexport const IntervalSchema = z\n .union([z.string(), PostgresIntervalSchema])\n .transform((interval) => {\n if (typeof interval === 'string') {\n interval = parsePostgresInterval(interval);\n }\n\n // This calculation matches Postgres's behavior when computing the number of\n // milliseconds in an interval with `EXTRACT(epoch from '...'::interval) * 1000`.\n // The noteworthy parts of this conversion are that 1 year = 365.25 days and\n // 1 month = 30 days.\n return (\n interval.years * INTERVAL_MS_PER_YEAR +\n interval.months * INTERVAL_MS_PER_MONTH +\n interval.days * INTERVAL_MS_PER_DAY +\n interval.hours * INTERVAL_MS_PER_HOUR +\n interval.minutes * INTERVAL_MS_PER_MINUTE +\n interval.seconds * INTERVAL_MS_PER_SECOND +\n interval.milliseconds\n );\n });\n\n/**\n * A Zod schema for a date string in ISO format.\n *\n * Accepts either a string or a Date object. If a string is passed, it is\n * validated and parsed as an ISO date string.\n *\n * Useful for parsing dates from JSON, which are always strings.\n */\nexport const DateFromISOString = z\n .union([z.string(), z.date()])\n .refine(\n (s) => {\n const date = new Date(s);\n return !Number.isNaN(date.getTime());\n },\n {\n message: 'must be a valid ISO date string',\n },\n )\n .transform((s) => new Date(s));\n\n/**\n * A Zod schema that coerces a non-empty string to an integer or an empty string to null.\n * This is useful for form number inputs that are not required but we do not want to\n * use an empty string to compute values.\n */\nexport const IntegerFromStringOrEmptySchema = z.preprocess(\n (value) => (value === '' ? null : value),\n z.union([z.null(), z.coerce.number().int()]),\n);\n\n/**\n * A Zod schema for an arrray of string values from either a string or an array of\n * strings.\n */\nexport const ArrayFromStringOrArraySchema = z\n .union([z.string(), z.array(z.string())])\n .transform((s) => {\n if (s === null) {\n return [];\n } else if (Array.isArray(s)) {\n return s;\n } else {\n return [s];\n }\n });\n"]}
|
package/dist/index.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { assert } from 'chai';
|
|
2
2
|
import parsePostgresInterval from 'postgres-interval';
|
|
3
|
-
import { IdSchema, IntervalSchema } from './index.js';
|
|
3
|
+
import { ArrayFromStringOrArraySchema, IdSchema, IntegerFromStringOrEmptySchema, IntervalSchema, } from './index.js';
|
|
4
4
|
describe('IdSchema', () => {
|
|
5
5
|
it('parses a valid id', () => {
|
|
6
6
|
const id = IdSchema.parse('123');
|
|
@@ -61,4 +61,40 @@ describe('IntervalSchema', () => {
|
|
|
61
61
|
assert.equal(interval, -37015506789);
|
|
62
62
|
});
|
|
63
63
|
});
|
|
64
|
+
describe('IntegerFromStringOrEmptySchema', () => {
|
|
65
|
+
it('parses a valid integer string', () => {
|
|
66
|
+
const result = IntegerFromStringOrEmptySchema.parse('123');
|
|
67
|
+
assert.equal(result, 123);
|
|
68
|
+
});
|
|
69
|
+
it('parses an empty string as null', () => {
|
|
70
|
+
const result = IntegerFromStringOrEmptySchema.parse('');
|
|
71
|
+
assert.equal(result, null);
|
|
72
|
+
});
|
|
73
|
+
it('rejects a non-integer string', () => {
|
|
74
|
+
const result = IntegerFromStringOrEmptySchema.safeParse('abc');
|
|
75
|
+
assert.isFalse(result.success);
|
|
76
|
+
});
|
|
77
|
+
it('rejects a decimal string', () => {
|
|
78
|
+
const result = IntegerFromStringOrEmptySchema.safeParse('123.45');
|
|
79
|
+
assert.isFalse(result.success);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
describe('ArrayFromStringOrArraySchema', () => {
|
|
83
|
+
it('parses a string to an array', () => {
|
|
84
|
+
const result = ArrayFromStringOrArraySchema.parse('a');
|
|
85
|
+
assert.deepEqual(result, ['a']);
|
|
86
|
+
});
|
|
87
|
+
it('parses an array to itself', () => {
|
|
88
|
+
const result = ArrayFromStringOrArraySchema.parse(['a', 'b', 'c']);
|
|
89
|
+
assert.deepEqual(result, ['a', 'b', 'c']);
|
|
90
|
+
});
|
|
91
|
+
it('rejects an integer', () => {
|
|
92
|
+
const result = ArrayFromStringOrArraySchema.safeParse(123);
|
|
93
|
+
assert.isFalse(result.success);
|
|
94
|
+
});
|
|
95
|
+
it('rejects an object', () => {
|
|
96
|
+
const result = ArrayFromStringOrArraySchema.safeParse({ a: 1 });
|
|
97
|
+
assert.isFalse(result.success);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
64
100
|
//# sourceMappingURL=index.test.js.map
|
package/dist/index.test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO,qBAAqB,MAAM,mBAAmB,CAAC;AAEtD,OAAO,
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO,qBAAqB,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EACL,4BAA4B,EAC5B,QAAQ,EACR,8BAA8B,EAC9B,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;IACxB,EAAE,CAAC,mBAAmB,EAAE,GAAG,EAAE;QAC3B,MAAM,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sBAAsB,EAAE,GAAG,EAAE;QAC9B,MAAM,EAAE,GAAG,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC3C,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uBAAuB,EAAE,GAAG,EAAE;QAC/B,MAAM,EAAE,GAAG,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAChD,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IA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;IAC5C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oBAAoB,EAAE,GAAG,EAAE;QAC5B,MAAM,MAAM,GAAG,4BAA4B,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAC3D,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mBAAmB,EAAE,GAAG,EAAE;QAC3B,MAAM,MAAM,GAAG,4BAA4B,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QAChE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { assert } from 'chai';\nimport parsePostgresInterval from 'postgres-interval';\n\nimport {\n ArrayFromStringOrArraySchema,\n IdSchema,\n IntegerFromStringOrEmptySchema,\n IntervalSchema,\n} from './index.js';\n\ndescribe('IdSchema', () => {\n it('parses a valid id', () => {\n const id = IdSchema.parse('123');\n assert.equal(id, '123');\n });\n\n it('parses a nullable id', () => {\n const id = IdSchema.nullable().parse(null);\n assert.equal(id, null);\n });\n\n it('parses an optional id', () => {\n const id = IdSchema.optional().parse(undefined);\n assert.equal(id, undefined);\n });\n\n it('rejects a negative ID', () => {\n const result = IdSchema.safeParse('-1');\n assert.isFalse(result.success);\n });\n\n it('rejects a non-numeric ID', () => {\n const result = IdSchema.safeParse('abc');\n assert.isFalse(result.success);\n });\n});\n\ndescribe('IntervalSchema', () => {\n it('handles a PostgresInterval object', () => {\n const interval = IntervalSchema.parse(parsePostgresInterval('1 year 2 months 3 days'));\n assert.equal(interval, 37000800000);\n });\n\n it('parses an interval with date', () => {\n const interval = IntervalSchema.parse('1 year 2 months 3 days');\n assert.equal(interval, 37000800000);\n });\n\n it('parses an interval with time', () => {\n const interval = IntervalSchema.parse('04:05:06.7');\n assert.equal(interval, 14706700);\n });\n\n it('parses an interval with microsecond-precision time', () => {\n const interval = IntervalSchema.parse('01:02:03.456789');\n assert.equal(interval, 3723456.789);\n });\n\n it('parses a complex interval', () => {\n const interval = IntervalSchema.parse('1 years 2 mons 3 days 04:05:06.789');\n assert.equal(interval, 37015506789);\n });\n\n it('parses interval with negative months', () => {\n const interval = IntervalSchema.parse('-10 mons 3 days 04:05:06.789');\n assert.equal(interval, -25646093211);\n });\n\n it('parses interval with negative years and months', () => {\n const interval = IntervalSchema.parse('-1 years -2 months 3 days 04:05:06.789');\n assert.equal(interval, -36467693211);\n });\n\n it('parses interval with negative years, months, and days', () => {\n const interval = IntervalSchema.parse('-1 years -2 months -3 days 04:05:06.789');\n assert.equal(interval, -36986093211);\n });\n\n it('parses a negative interval', () => {\n const interval = IntervalSchema.parse('-1 years -2 months -3 days -04:05:06.789');\n assert.equal(interval, -37015506789);\n });\n});\n\ndescribe('IntegerFromStringOrEmptySchema', () => {\n it('parses a valid integer string', () => {\n const result = IntegerFromStringOrEmptySchema.parse('123');\n assert.equal(result, 123);\n });\n\n it('parses an empty string as null', () => {\n const result = IntegerFromStringOrEmptySchema.parse('');\n assert.equal(result, null);\n });\n\n it('rejects a non-integer string', () => {\n const result = IntegerFromStringOrEmptySchema.safeParse('abc');\n assert.isFalse(result.success);\n });\n\n it('rejects a decimal string', () => {\n const result = IntegerFromStringOrEmptySchema.safeParse('123.45');\n assert.isFalse(result.success);\n });\n});\n\ndescribe('ArrayFromStringOrArraySchema', () => {\n it('parses a string to an array', () => {\n const result = ArrayFromStringOrArraySchema.parse('a');\n assert.deepEqual(result, ['a']);\n });\n\n it('parses an array to itself', () => {\n const result = ArrayFromStringOrArraySchema.parse(['a', 'b', 'c']);\n assert.deepEqual(result, ['a', 'b', 'c']);\n });\n\n it('rejects an integer', () => {\n const result = ArrayFromStringOrArraySchema.safeParse(123);\n assert.isFalse(result.success);\n });\n\n it('rejects an object', () => {\n const result = ArrayFromStringOrArraySchema.safeParse({ a: 1 });\n assert.isFalse(result.success);\n });\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/zod",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
22
22
|
"@types/chai": "^5.2.1",
|
|
23
23
|
"@types/mocha": "^10.0.10",
|
|
24
|
-
"@types/node": "^20.17.
|
|
24
|
+
"@types/node": "^20.17.32",
|
|
25
25
|
"c8": "^10.1.3",
|
|
26
26
|
"chai": "^5.2.0",
|
|
27
27
|
"mocha": "^11.1.0",
|
|
28
|
-
"tsx": "^4.19.
|
|
28
|
+
"tsx": "^4.19.4",
|
|
29
29
|
"typescript": "^5.8.3"
|
|
30
30
|
},
|
|
31
31
|
"c8": {
|
package/src/index.test.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { assert } from 'chai';
|
|
2
2
|
import parsePostgresInterval from 'postgres-interval';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
ArrayFromStringOrArraySchema,
|
|
6
|
+
IdSchema,
|
|
7
|
+
IntegerFromStringOrEmptySchema,
|
|
8
|
+
IntervalSchema,
|
|
9
|
+
} from './index.js';
|
|
5
10
|
|
|
6
11
|
describe('IdSchema', () => {
|
|
7
12
|
it('parses a valid id', () => {
|
|
@@ -76,3 +81,47 @@ describe('IntervalSchema', () => {
|
|
|
76
81
|
assert.equal(interval, -37015506789);
|
|
77
82
|
});
|
|
78
83
|
});
|
|
84
|
+
|
|
85
|
+
describe('IntegerFromStringOrEmptySchema', () => {
|
|
86
|
+
it('parses a valid integer string', () => {
|
|
87
|
+
const result = IntegerFromStringOrEmptySchema.parse('123');
|
|
88
|
+
assert.equal(result, 123);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('parses an empty string as null', () => {
|
|
92
|
+
const result = IntegerFromStringOrEmptySchema.parse('');
|
|
93
|
+
assert.equal(result, null);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('rejects a non-integer string', () => {
|
|
97
|
+
const result = IntegerFromStringOrEmptySchema.safeParse('abc');
|
|
98
|
+
assert.isFalse(result.success);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('rejects a decimal string', () => {
|
|
102
|
+
const result = IntegerFromStringOrEmptySchema.safeParse('123.45');
|
|
103
|
+
assert.isFalse(result.success);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('ArrayFromStringOrArraySchema', () => {
|
|
108
|
+
it('parses a string to an array', () => {
|
|
109
|
+
const result = ArrayFromStringOrArraySchema.parse('a');
|
|
110
|
+
assert.deepEqual(result, ['a']);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('parses an array to itself', () => {
|
|
114
|
+
const result = ArrayFromStringOrArraySchema.parse(['a', 'b', 'c']);
|
|
115
|
+
assert.deepEqual(result, ['a', 'b', 'c']);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('rejects an integer', () => {
|
|
119
|
+
const result = ArrayFromStringOrArraySchema.safeParse(123);
|
|
120
|
+
assert.isFalse(result.success);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('rejects an object', () => {
|
|
124
|
+
const result = ArrayFromStringOrArraySchema.safeParse({ a: 1 });
|
|
125
|
+
assert.isFalse(result.success);
|
|
126
|
+
});
|
|
127
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -102,3 +102,29 @@ export const DateFromISOString = z
|
|
|
102
102
|
},
|
|
103
103
|
)
|
|
104
104
|
.transform((s) => new Date(s));
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A Zod schema that coerces a non-empty string to an integer or an empty string to null.
|
|
108
|
+
* This is useful for form number inputs that are not required but we do not want to
|
|
109
|
+
* use an empty string to compute values.
|
|
110
|
+
*/
|
|
111
|
+
export const IntegerFromStringOrEmptySchema = z.preprocess(
|
|
112
|
+
(value) => (value === '' ? null : value),
|
|
113
|
+
z.union([z.null(), z.coerce.number().int()]),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A Zod schema for an arrray of string values from either a string or an array of
|
|
118
|
+
* strings.
|
|
119
|
+
*/
|
|
120
|
+
export const ArrayFromStringOrArraySchema = z
|
|
121
|
+
.union([z.string(), z.array(z.string())])
|
|
122
|
+
.transform((s) => {
|
|
123
|
+
if (s === null) {
|
|
124
|
+
return [];
|
|
125
|
+
} else if (Array.isArray(s)) {
|
|
126
|
+
return s;
|
|
127
|
+
} else {
|
|
128
|
+
return [s];
|
|
129
|
+
}
|
|
130
|
+
});
|