@awsless/validate 0.0.4 → 0.0.6

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/dist/index.cjs CHANGED
@@ -15,221 +15,233 @@ var __copyProps = (to, from, except, desc) => {
15
15
  }
16
16
  return to;
17
17
  };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
20
 
20
21
  // src/index.ts
21
22
  var src_exports = {};
22
23
  __export(src_exports, {
23
- Struct: () => import_superstruct7.Struct,
24
- StructError: () => import_superstruct7.StructError,
25
- any: () => import_superstruct7.any,
26
- array: () => import_superstruct7.array,
27
- assert: () => import_superstruct7.assert,
28
- assign: () => import_superstruct7.assign,
29
24
  bigfloat: () => bigfloat,
30
- bigint: () => import_superstruct7.bigint,
31
- boolean: () => import_superstruct7.boolean,
32
- coerce: () => import_superstruct7.coerce,
33
- create: () => import_superstruct7.create,
34
25
  date: () => date,
35
- defaulted: () => import_superstruct7.defaulted,
36
- define: () => import_superstruct7.define,
37
- deprecated: () => import_superstruct7.deprecated,
38
- dynamic: () => import_superstruct7.dynamic,
39
- empty: () => import_superstruct7.empty,
40
- enums: () => import_superstruct7.enums,
41
- func: () => import_superstruct7.func,
42
- instance: () => import_superstruct7.instance,
43
- integer: () => import_superstruct7.integer,
44
- intersection: () => import_superstruct7.intersection,
45
- is: () => import_superstruct7.is,
26
+ dynamoDbStream: () => dynamoDbStream,
46
27
  json: () => json,
47
- lazy: () => import_superstruct7.lazy,
48
- literal: () => import_superstruct7.literal,
49
- lowercase: () => lowercase,
50
- map: () => import_superstruct7.map,
51
- mask: () => import_superstruct7.mask,
52
- max: () => import_superstruct7.max,
53
- min: () => import_superstruct7.min,
54
- never: () => import_superstruct7.never,
55
- nonempty: () => import_superstruct7.nonempty,
56
- nullable: () => import_superstruct7.nullable,
57
- number: () => import_superstruct7.number,
58
- object: () => import_superstruct7.object,
59
- omit: () => import_superstruct7.omit,
60
- optional: () => import_superstruct7.optional,
61
- partial: () => import_superstruct7.partial,
62
- pattern: () => import_superstruct7.pattern,
63
- pick: () => import_superstruct7.pick,
64
28
  positive: () => positive,
65
29
  precision: () => precision,
66
- record: () => import_superstruct7.record,
67
- refine: () => import_superstruct7.refine,
68
- regexp: () => import_superstruct7.regexp,
69
- set: () => import_superstruct7.set,
70
- size: () => import_superstruct7.size,
71
- string: () => import_superstruct7.string,
72
- struct: () => import_superstruct7.struct,
73
- trimmed: () => import_superstruct7.trimmed,
74
- tuple: () => import_superstruct7.tuple,
75
- type: () => import_superstruct7.type,
76
- union: () => import_superstruct7.union,
30
+ snsTopic: () => snsTopic,
31
+ sqsQueue: () => sqsQueue,
77
32
  unique: () => unique,
78
- unknown: () => import_superstruct7.unknown,
79
- uppercase: () => uppercase,
80
33
  uuid: () => uuid
81
34
  });
82
35
  module.exports = __toCommonJS(src_exports);
36
+ __reExport(src_exports, require("valibot"), module.exports);
83
37
 
84
- // src/types/bigfloat.ts
85
- var import_superstruct = require("superstruct");
86
- var import_big_float = require("@awsless/big-float");
87
- var bigfloat = () => {
88
- const base = (0, import_superstruct.define)("bigfloat", (value) => {
89
- return value instanceof import_big_float.BigFloat || "Invalid number";
90
- });
91
- const bigFloatLike = (0, import_superstruct.coerce)(base, (0, import_superstruct.object)({
92
- exponent: (0, import_superstruct.number)(),
93
- coefficient: (0, import_superstruct.bigint)()
94
- }), (value) => {
95
- return new import_big_float.BigFloat(value);
96
- });
97
- return (0, import_superstruct.coerce)(bigFloatLike, (0, import_superstruct.union)([(0, import_superstruct.string)(), (0, import_superstruct.number)()]), (value) => {
98
- if (typeof value === "string" && value !== "" || typeof value === "number") {
99
- if (!isNaN(Number(value))) {
100
- return new import_big_float.BigFloat(value);
38
+ // src/schema/json.ts
39
+ var import_valibot = require("valibot");
40
+ var json = (schema) => {
41
+ return (0, import_valibot.transform)(
42
+ (0, import_valibot.string)(),
43
+ (value) => {
44
+ try {
45
+ return JSON.parse(value);
46
+ } catch (error) {
47
+ return null;
101
48
  }
102
- }
103
- return null;
104
- });
105
- };
106
- var positive = (struct2) => {
107
- const expected = `Expected a positive ${struct2.type}`;
108
- const ZERO = new import_big_float.BigFloat(0);
109
- return (0, import_superstruct.refine)(struct2, "positive", (value) => {
110
- return (0, import_big_float.gt)(value, ZERO) || `${expected} but received '${value}'`;
111
- });
112
- };
113
- var precision = (struct2, decimals) => {
114
- const expected = `Expected a ${struct2.type}`;
115
- return (0, import_superstruct.refine)(struct2, "precision", (value) => {
116
- const big = new import_big_float.BigFloat(value);
117
- return -big.exponent <= decimals || `${expected} with ${decimals} decimals`;
118
- });
49
+ },
50
+ schema
51
+ );
119
52
  };
120
53
 
121
- // src/types/date.ts
122
- var import_superstruct2 = require("superstruct");
123
- var date = () => {
124
- return (0, import_superstruct2.coerce)((0, import_superstruct2.date)(), (0, import_superstruct2.string)(), (value) => {
125
- return new Date(value);
126
- });
127
- };
54
+ // src/schema/bigfloat.ts
55
+ var import_big_float = require("@awsless/big-float");
56
+ var import_valibot2 = require("valibot");
57
+ var make = (value) => new import_big_float.BigFloat(value);
58
+ function bigfloat(arg1, arg2) {
59
+ const [msg, pipe] = (0, import_valibot2.getDefaultArgs)(arg1, arg2);
60
+ const error = msg ?? "Invalid bigfloat";
61
+ return (0, import_valibot2.union)(
62
+ [
63
+ (0, import_valibot2.instance)(import_big_float.BigFloat, pipe),
64
+ (0, import_valibot2.transform)(
65
+ (0, import_valibot2.string)([
66
+ (input) => {
67
+ if (input === "" || isNaN(Number(input))) {
68
+ return (0, import_valibot2.getPipeIssues)("bigfloat", error, input);
69
+ }
70
+ return (0, import_valibot2.getOutput)(input);
71
+ }
72
+ ]),
73
+ make,
74
+ pipe
75
+ ),
76
+ (0, import_valibot2.transform)((0, import_valibot2.number)(), make, pipe),
77
+ (0, import_valibot2.transform)(
78
+ (0, import_valibot2.object)({
79
+ exponent: (0, import_valibot2.number)(),
80
+ coefficient: (0, import_valibot2.bigint)()
81
+ }),
82
+ make,
83
+ pipe
84
+ )
85
+ ],
86
+ error
87
+ );
88
+ }
89
+
90
+ // src/schema/date.ts
91
+ var import_valibot3 = require("valibot");
92
+ function date(arg1, arg2) {
93
+ const [error, pipe] = (0, import_valibot3.getDefaultArgs)(arg1, arg2);
94
+ return (0, import_valibot3.union)(
95
+ [
96
+ (0, import_valibot3.date)(pipe),
97
+ (0, import_valibot3.transform)(
98
+ (0, import_valibot3.string)(),
99
+ (input) => {
100
+ return new Date(input);
101
+ },
102
+ (0, import_valibot3.date)(pipe)
103
+ )
104
+ ],
105
+ error ?? "Invalid date"
106
+ );
107
+ }
128
108
 
129
- // src/types/uuid.ts
130
- var import_superstruct3 = require("superstruct");
109
+ // src/schema/uuid.ts
110
+ var import_valibot4 = require("valibot");
131
111
  var uuid = () => {
132
- return (0, import_superstruct3.define)("uuid", (value) => {
133
- return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(String(value));
134
- });
112
+ return (0, import_valibot4.transform)((0, import_valibot4.string)([(0, import_valibot4.uuid)()]), (v) => v);
135
113
  };
136
114
 
137
- // src/types/json.ts
138
- var import_superstruct4 = require("superstruct");
139
- var json = (struct2) => {
140
- return (0, import_superstruct4.coerce)(struct2, (0, import_superstruct4.string)(), (value) => {
141
- try {
142
- return JSON.parse(value);
143
- } catch (error) {
144
- return value;
145
- }
146
- });
115
+ // src/schema/aws/sqs-queue.ts
116
+ var import_valibot5 = require("valibot");
117
+ var sqsQueue = (body) => {
118
+ const schema = body ?? (0, import_valibot5.unknown)();
119
+ return (0, import_valibot5.union)(
120
+ [
121
+ (0, import_valibot5.transform)(schema, (input) => [input]),
122
+ (0, import_valibot5.array)(schema),
123
+ (0, import_valibot5.transform)(
124
+ (0, import_valibot5.object)({
125
+ Records: (0, import_valibot5.array)(
126
+ (0, import_valibot5.object)({
127
+ body: json(schema)
128
+ })
129
+ )
130
+ }),
131
+ (input) => input.Records.map((record) => {
132
+ return record.body;
133
+ })
134
+ )
135
+ ],
136
+ "Invalid SQS Queue input"
137
+ );
147
138
  };
148
139
 
149
- // src/types/string.ts
150
- var import_superstruct5 = require("superstruct");
151
- var lowercase = (struct2) => {
152
- return (0, import_superstruct5.coerce)(struct2, (0, import_superstruct5.string)(), (value) => value.toLowerCase());
140
+ // src/schema/aws/sns-topic.ts
141
+ var import_valibot6 = require("valibot");
142
+ var snsTopic = (body) => {
143
+ const schema = body ?? (0, import_valibot6.unknown)();
144
+ return (0, import_valibot6.union)(
145
+ [
146
+ (0, import_valibot6.transform)(schema, (input) => [input]),
147
+ (0, import_valibot6.array)(schema),
148
+ (0, import_valibot6.transform)(
149
+ (0, import_valibot6.object)({
150
+ Records: (0, import_valibot6.array)(
151
+ (0, import_valibot6.object)({
152
+ Sns: (0, import_valibot6.object)({
153
+ Message: json(schema)
154
+ })
155
+ })
156
+ )
157
+ }),
158
+ (input) => input.Records.map((record) => {
159
+ return record.Sns.Message;
160
+ })
161
+ )
162
+ ],
163
+ "Invalid SNS Topic input"
164
+ );
153
165
  };
154
- var uppercase = (struct2) => {
155
- return (0, import_superstruct5.coerce)(struct2, (0, import_superstruct5.string)(), (value) => value.toUpperCase());
166
+
167
+ // src/schema/aws/dynamodb-stream.ts
168
+ var import_valibot7 = require("valibot");
169
+ var dynamoDbStream = (table) => {
170
+ const marshall = () => (0, import_valibot7.transform)((0, import_valibot7.unknown)(), (value) => table.unmarshall(value));
171
+ return (0, import_valibot7.transform)(
172
+ (0, import_valibot7.object)(
173
+ {
174
+ Records: (0, import_valibot7.array)(
175
+ (0, import_valibot7.object)({
176
+ eventName: (0, import_valibot7.picklist)(["MODIFY", "INSERT", "REMOVE"]),
177
+ dynamodb: (0, import_valibot7.object)({
178
+ Keys: marshall(),
179
+ OldImage: (0, import_valibot7.optional)(marshall()),
180
+ NewImage: (0, import_valibot7.optional)(marshall())
181
+ })
182
+ })
183
+ )
184
+ },
185
+ "Invalid DynamoDB Stream input"
186
+ ),
187
+ (input) => {
188
+ return input.Records.map((record) => {
189
+ const item = record;
190
+ return {
191
+ event: record.eventName,
192
+ keys: item.dynamodb.Keys,
193
+ old: item.dynamodb.OldImage,
194
+ new: item.dynamodb.NewImage
195
+ };
196
+ });
197
+ }
198
+ );
156
199
  };
157
200
 
158
- // src/types/array.ts
159
- var import_superstruct6 = require("superstruct");
160
- function unique(struct2, compare = (a, b) => a === b) {
161
- return (0, import_superstruct6.refine)(struct2, "unique", (value) => {
162
- for (const x in value) {
163
- for (const y in value) {
164
- if (x !== y && compare(value[x], value[y])) {
165
- return `Expected a ${struct2.type} with unique values, but received "${value}"`;
201
+ // src/validation/positive.ts
202
+ var import_big_float2 = require("@awsless/big-float");
203
+ var import_valibot8 = require("valibot");
204
+ function positive(error) {
205
+ return (input) => {
206
+ return (0, import_big_float2.gt)(input, import_big_float2.ZERO) ? (0, import_valibot8.getOutput)(input) : (0, import_valibot8.getPipeIssues)("positive", error ?? "Invalid positive number", input);
207
+ };
208
+ }
209
+
210
+ // src/validation/precision.ts
211
+ var import_big_float3 = require("@awsless/big-float");
212
+ var import_valibot9 = require("valibot");
213
+ function precision(decimals, error) {
214
+ return (input) => {
215
+ const big = new import_big_float3.BigFloat(input.toString());
216
+ return -big.exponent <= decimals ? (0, import_valibot9.getOutput)(input) : (0, import_valibot9.getPipeIssues)("precision", error ?? `Invalid ${decimals} precision number`, input);
217
+ };
218
+ }
219
+
220
+ // src/validation/unique.ts
221
+ var import_valibot10 = require("valibot");
222
+ function unique(compare = (a, b) => a === b, error) {
223
+ return (input) => {
224
+ for (const x in input) {
225
+ for (const y in input) {
226
+ if (x !== y && compare(input[x], input[y])) {
227
+ return (0, import_valibot10.getPipeIssues)("unique", error ?? "None unique array", input);
166
228
  }
167
229
  }
168
230
  }
169
- return true;
170
- });
231
+ return (0, import_valibot10.getOutput)(input);
232
+ };
171
233
  }
172
-
173
- // src/index.ts
174
- var import_superstruct7 = require("superstruct");
175
234
  // Annotate the CommonJS export names for ESM import in node:
176
235
  0 && (module.exports = {
177
- Struct,
178
- StructError,
179
- any,
180
- array,
181
- assert,
182
- assign,
183
236
  bigfloat,
184
- bigint,
185
- boolean,
186
- coerce,
187
- create,
188
237
  date,
189
- defaulted,
190
- define,
191
- deprecated,
192
- dynamic,
193
- empty,
194
- enums,
195
- func,
196
- instance,
197
- integer,
198
- intersection,
199
- is,
238
+ dynamoDbStream,
200
239
  json,
201
- lazy,
202
- literal,
203
- lowercase,
204
- map,
205
- mask,
206
- max,
207
- min,
208
- never,
209
- nonempty,
210
- nullable,
211
- number,
212
- object,
213
- omit,
214
- optional,
215
- partial,
216
- pattern,
217
- pick,
218
240
  positive,
219
241
  precision,
220
- record,
221
- refine,
222
- regexp,
223
- set,
224
- size,
225
- string,
226
- struct,
227
- trimmed,
228
- tuple,
229
- type,
230
- union,
242
+ snsTopic,
243
+ sqsQueue,
231
244
  unique,
232
- unknown,
233
- uppercase,
234
- uuid
245
+ uuid,
246
+ ...require("valibot")
235
247
  });
@@ -0,0 +1,64 @@
1
+ import { BaseSchema, SchemaWithTransform, StringSchema, Output, Pipe, ErrorMessage, UnknownSchema, Input, PipeResult } from 'valibot';
2
+ export * from 'valibot';
3
+ import { BigFloat } from '@awsless/big-float';
4
+ import { UUID } from 'crypto';
5
+ import { TableDefinition, PrimaryKey } from '@awsless/dynamodb';
6
+
7
+ type JsonSchema<T extends BaseSchema> = SchemaWithTransform<StringSchema, Output<T>>;
8
+ declare const json: <T extends BaseSchema<any, any>>(schema: T) => JsonSchema<T>;
9
+
10
+ type BigFloatSchema = BaseSchema<string | number | BigFloat | {
11
+ exponent: number;
12
+ coefficient: bigint;
13
+ }, BigFloat>;
14
+ declare function bigfloat(pipe?: Pipe<BigFloat>): BigFloatSchema;
15
+ declare function bigfloat(error?: ErrorMessage, pipe?: Pipe<BigFloat>): BigFloatSchema;
16
+
17
+ type DateSchema = BaseSchema<string | Date, Date>;
18
+ declare function date(pipe?: Pipe<Date>): DateSchema;
19
+ declare function date(error?: ErrorMessage, pipe?: Pipe<Date>): DateSchema;
20
+
21
+ type UuidSchema = SchemaWithTransform<StringSchema | BaseSchema<UUID>, UUID>;
22
+ declare const uuid: () => UuidSchema;
23
+
24
+ type SqsQueueSchema<S extends BaseSchema = UnknownSchema> = BaseSchema<Input<S> | Input<S>[] | {
25
+ Records: {
26
+ body: string | Input<S>;
27
+ }[];
28
+ }, Output<S>[]>;
29
+ declare const sqsQueue: <S extends BaseSchema<any, any> = UnknownSchema<unknown>>(body?: S | undefined) => SqsQueueSchema<S>;
30
+
31
+ type SnsTopicSchema<S extends BaseSchema = UnknownSchema> = BaseSchema<Input<S> | Input<S>[] | {
32
+ Records: {
33
+ Sns: {
34
+ Message: string | Input<S>;
35
+ };
36
+ }[];
37
+ }, Output<S>[]>;
38
+ declare const snsTopic: <S extends BaseSchema<any, any> = UnknownSchema<unknown>>(body?: S | undefined) => SnsTopicSchema<S>;
39
+
40
+ type EventName = 'MODIFY' | 'INSERT' | 'REMOVE';
41
+ type DynamoDBStreamSchema<T extends TableDefinition<any, any, any, any>> = BaseSchema<{
42
+ Records: {
43
+ eventName: EventName;
44
+ dynamodb: {
45
+ Keys: unknown;
46
+ OldImage?: unknown;
47
+ NewImage?: unknown;
48
+ };
49
+ }[];
50
+ }, {
51
+ event: EventName;
52
+ keys: PrimaryKey<T>;
53
+ old?: Partial<T['schema']['OUTPUT']>;
54
+ new?: Partial<T['schema']['OUTPUT']>;
55
+ }[]>;
56
+ declare const dynamoDbStream: <T extends TableDefinition<any, any, any, any>>(table: T) => DynamoDBStreamSchema<T>;
57
+
58
+ declare function positive<T extends BigFloat | number>(error?: ErrorMessage): (input: T) => PipeResult<T>;
59
+
60
+ declare function precision<T extends BigFloat | number>(decimals: number, error?: ErrorMessage): (input: T) => PipeResult<T>;
61
+
62
+ declare function unique<T extends any[]>(compare?: (a: T[number], b: T[number]) => boolean, error?: ErrorMessage): (input: T) => PipeResult<T>;
63
+
64
+ export { BigFloatSchema, DateSchema, DynamoDBStreamSchema, JsonSchema, SnsTopicSchema, SqsQueueSchema, UuidSchema, bigfloat, date, dynamoDbStream, json, positive, precision, snsTopic, sqsQueue, unique, uuid };
package/dist/index.d.ts CHANGED
@@ -1,21 +1,64 @@
1
- import { Struct } from 'superstruct';
2
- export { Coercer, Context, Describe, Failure, Infer, Refiner, Result, Struct, StructError, Validator, any, array, assert, assign, bigint, boolean, coerce, create, defaulted, define, deprecated, dynamic, empty, enums, func, instance, integer, intersection, is, lazy, literal, map, mask, max, min, never, nonempty, nullable, number, object, omit, optional, partial, pattern, pick, record, refine, regexp, set, size, string, struct, trimmed, tuple, type, union, unknown } from 'superstruct';
1
+ import { BaseSchema, SchemaWithTransform, StringSchema, Output, Pipe, ErrorMessage, UnknownSchema, Input, PipeResult } from 'valibot';
2
+ export * from 'valibot';
3
3
  import { BigFloat } from '@awsless/big-float';
4
4
  import { UUID } from 'crypto';
5
+ import { TableDefinition, PrimaryKey } from '@awsless/dynamodb';
5
6
 
6
- declare const bigfloat: () => Struct<BigFloat, null>;
7
- declare const positive: <T extends number | BigFloat, S extends unknown>(struct: Struct<T, S>) => Struct<T, S>;
8
- declare const precision: <T extends number | BigFloat, S extends unknown>(struct: Struct<T, S>, decimals: number) => Struct<T, S>;
7
+ type JsonSchema<T extends BaseSchema> = SchemaWithTransform<StringSchema, Output<T>>;
8
+ declare const json: <T extends BaseSchema<any, any>>(schema: T) => JsonSchema<T>;
9
9
 
10
- declare const date: () => Struct<Date, null>;
10
+ type BigFloatSchema = BaseSchema<string | number | BigFloat | {
11
+ exponent: number;
12
+ coefficient: bigint;
13
+ }, BigFloat>;
14
+ declare function bigfloat(pipe?: Pipe<BigFloat>): BigFloatSchema;
15
+ declare function bigfloat(error?: ErrorMessage, pipe?: Pipe<BigFloat>): BigFloatSchema;
11
16
 
12
- declare const uuid: () => Struct<UUID, null>;
17
+ type DateSchema = BaseSchema<string | Date, Date>;
18
+ declare function date(pipe?: Pipe<Date>): DateSchema;
19
+ declare function date(error?: ErrorMessage, pipe?: Pipe<Date>): DateSchema;
13
20
 
14
- declare const json: <T, S>(struct: Struct<T, S>) => Struct<T, S>;
21
+ type UuidSchema = SchemaWithTransform<StringSchema | BaseSchema<UUID>, UUID>;
22
+ declare const uuid: () => UuidSchema;
15
23
 
16
- declare const lowercase: <T, S>(struct: Struct<T, S>) => Struct<T, S>;
17
- declare const uppercase: <T, S>(struct: Struct<T, S>) => Struct<T, S>;
24
+ type SqsQueueSchema<S extends BaseSchema = UnknownSchema> = BaseSchema<Input<S> | Input<S>[] | {
25
+ Records: {
26
+ body: string | Input<S>;
27
+ }[];
28
+ }, Output<S>[]>;
29
+ declare const sqsQueue: <S extends BaseSchema<any, any> = UnknownSchema<unknown>>(body?: S | undefined) => SqsQueueSchema<S>;
18
30
 
19
- declare function unique<T extends any[], S extends any>(struct: Struct<T, S>, compare?: (a: T[number], b: T[number]) => boolean): Struct<T, S>;
31
+ type SnsTopicSchema<S extends BaseSchema = UnknownSchema> = BaseSchema<Input<S> | Input<S>[] | {
32
+ Records: {
33
+ Sns: {
34
+ Message: string | Input<S>;
35
+ };
36
+ }[];
37
+ }, Output<S>[]>;
38
+ declare const snsTopic: <S extends BaseSchema<any, any> = UnknownSchema<unknown>>(body?: S | undefined) => SnsTopicSchema<S>;
20
39
 
21
- export { bigfloat, date, json, lowercase, positive, precision, unique, uppercase, uuid };
40
+ type EventName = 'MODIFY' | 'INSERT' | 'REMOVE';
41
+ type DynamoDBStreamSchema<T extends TableDefinition<any, any, any, any>> = BaseSchema<{
42
+ Records: {
43
+ eventName: EventName;
44
+ dynamodb: {
45
+ Keys: unknown;
46
+ OldImage?: unknown;
47
+ NewImage?: unknown;
48
+ };
49
+ }[];
50
+ }, {
51
+ event: EventName;
52
+ keys: PrimaryKey<T>;
53
+ old?: Partial<T['schema']['OUTPUT']>;
54
+ new?: Partial<T['schema']['OUTPUT']>;
55
+ }[]>;
56
+ declare const dynamoDbStream: <T extends TableDefinition<any, any, any, any>>(table: T) => DynamoDBStreamSchema<T>;
57
+
58
+ declare function positive<T extends BigFloat | number>(error?: ErrorMessage): (input: T) => PipeResult<T>;
59
+
60
+ declare function precision<T extends BigFloat | number>(decimals: number, error?: ErrorMessage): (input: T) => PipeResult<T>;
61
+
62
+ declare function unique<T extends any[]>(compare?: (a: T[number], b: T[number]) => boolean, error?: ErrorMessage): (input: T) => PipeResult<T>;
63
+
64
+ export { BigFloatSchema, DateSchema, DynamoDBStreamSchema, JsonSchema, SnsTopicSchema, SqsQueueSchema, UuidSchema, bigfloat, date, dynamoDbStream, json, positive, precision, snsTopic, sqsQueue, unique, uuid };
package/dist/index.js CHANGED
@@ -1,201 +1,222 @@
1
- // src/types/bigfloat.ts
2
- import { coerce, define, number, string, union, refine, object, bigint } from "superstruct";
3
- import { BigFloat, gt } from "@awsless/big-float";
4
- var bigfloat = () => {
5
- const base = define("bigfloat", (value) => {
6
- return value instanceof BigFloat || "Invalid number";
7
- });
8
- const bigFloatLike = coerce(base, object({
9
- exponent: number(),
10
- coefficient: bigint()
11
- }), (value) => {
12
- return new BigFloat(value);
13
- });
14
- return coerce(bigFloatLike, union([string(), number()]), (value) => {
15
- if (typeof value === "string" && value !== "" || typeof value === "number") {
16
- if (!isNaN(Number(value))) {
17
- return new BigFloat(value);
1
+ // src/index.ts
2
+ export * from "valibot";
3
+
4
+ // src/schema/json.ts
5
+ import { string, transform } from "valibot";
6
+ var json = (schema) => {
7
+ return transform(
8
+ string(),
9
+ (value) => {
10
+ try {
11
+ return JSON.parse(value);
12
+ } catch (error) {
13
+ return null;
18
14
  }
19
- }
20
- return null;
21
- });
22
- };
23
- var positive = (struct2) => {
24
- const expected = `Expected a positive ${struct2.type}`;
25
- const ZERO = new BigFloat(0);
26
- return refine(struct2, "positive", (value) => {
27
- return gt(value, ZERO) || `${expected} but received '${value}'`;
28
- });
29
- };
30
- var precision = (struct2, decimals) => {
31
- const expected = `Expected a ${struct2.type}`;
32
- return refine(struct2, "precision", (value) => {
33
- const big = new BigFloat(value);
34
- return -big.exponent <= decimals || `${expected} with ${decimals} decimals`;
35
- });
15
+ },
16
+ schema
17
+ );
36
18
  };
37
19
 
38
- // src/types/date.ts
39
- import { coerce as coerce2, date as sdate, string as string2 } from "superstruct";
40
- var date = () => {
41
- return coerce2(sdate(), string2(), (value) => {
42
- return new Date(value);
43
- });
44
- };
20
+ // src/schema/bigfloat.ts
21
+ import { BigFloat } from "@awsless/big-float";
22
+ import {
23
+ bigint,
24
+ getDefaultArgs,
25
+ instance,
26
+ number,
27
+ object,
28
+ string as string2,
29
+ transform as transform2,
30
+ union,
31
+ getPipeIssues,
32
+ getOutput
33
+ } from "valibot";
34
+ var make = (value) => new BigFloat(value);
35
+ function bigfloat(arg1, arg2) {
36
+ const [msg, pipe] = getDefaultArgs(arg1, arg2);
37
+ const error = msg ?? "Invalid bigfloat";
38
+ return union(
39
+ [
40
+ instance(BigFloat, pipe),
41
+ transform2(
42
+ string2([
43
+ (input) => {
44
+ if (input === "" || isNaN(Number(input))) {
45
+ return getPipeIssues("bigfloat", error, input);
46
+ }
47
+ return getOutput(input);
48
+ }
49
+ ]),
50
+ make,
51
+ pipe
52
+ ),
53
+ transform2(number(), make, pipe),
54
+ transform2(
55
+ object({
56
+ exponent: number(),
57
+ coefficient: bigint()
58
+ }),
59
+ make,
60
+ pipe
61
+ )
62
+ ],
63
+ error
64
+ );
65
+ }
45
66
 
46
- // src/types/uuid.ts
47
- import { define as define2 } from "superstruct";
67
+ // src/schema/date.ts
68
+ import { getDefaultArgs as getDefaultArgs2, date as base, string as string3, union as union2, transform as transform3 } from "valibot";
69
+ function date(arg1, arg2) {
70
+ const [error, pipe] = getDefaultArgs2(arg1, arg2);
71
+ return union2(
72
+ [
73
+ base(pipe),
74
+ transform3(
75
+ string3(),
76
+ (input) => {
77
+ return new Date(input);
78
+ },
79
+ base(pipe)
80
+ )
81
+ ],
82
+ error ?? "Invalid date"
83
+ );
84
+ }
85
+
86
+ // src/schema/uuid.ts
87
+ import { string as string4, uuid as base2, transform as transform4 } from "valibot";
48
88
  var uuid = () => {
49
- return define2("uuid", (value) => {
50
- return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(String(value));
51
- });
89
+ return transform4(string4([base2()]), (v) => v);
52
90
  };
53
91
 
54
- // src/types/json.ts
55
- import { coerce as coerce3, string as string3 } from "superstruct";
56
- var json = (struct2) => {
57
- return coerce3(struct2, string3(), (value) => {
58
- try {
59
- return JSON.parse(value);
60
- } catch (error) {
61
- return value;
62
- }
63
- });
92
+ // src/schema/aws/sqs-queue.ts
93
+ import { array, object as object2, transform as transform5, union as union3, unknown } from "valibot";
94
+ var sqsQueue = (body) => {
95
+ const schema = body ?? unknown();
96
+ return union3(
97
+ [
98
+ transform5(schema, (input) => [input]),
99
+ array(schema),
100
+ transform5(
101
+ object2({
102
+ Records: array(
103
+ object2({
104
+ body: json(schema)
105
+ })
106
+ )
107
+ }),
108
+ (input) => input.Records.map((record) => {
109
+ return record.body;
110
+ })
111
+ )
112
+ ],
113
+ "Invalid SQS Queue input"
114
+ );
64
115
  };
65
116
 
66
- // src/types/string.ts
67
- import { coerce as coerce4, string as string4 } from "superstruct";
68
- var lowercase = (struct2) => {
69
- return coerce4(struct2, string4(), (value) => value.toLowerCase());
117
+ // src/schema/aws/sns-topic.ts
118
+ import { array as array2, object as object3, transform as transform6, union as union4, unknown as unknown2 } from "valibot";
119
+ var snsTopic = (body) => {
120
+ const schema = body ?? unknown2();
121
+ return union4(
122
+ [
123
+ transform6(schema, (input) => [input]),
124
+ array2(schema),
125
+ transform6(
126
+ object3({
127
+ Records: array2(
128
+ object3({
129
+ Sns: object3({
130
+ Message: json(schema)
131
+ })
132
+ })
133
+ )
134
+ }),
135
+ (input) => input.Records.map((record) => {
136
+ return record.Sns.Message;
137
+ })
138
+ )
139
+ ],
140
+ "Invalid SNS Topic input"
141
+ );
70
142
  };
71
- var uppercase = (struct2) => {
72
- return coerce4(struct2, string4(), (value) => value.toUpperCase());
143
+
144
+ // src/schema/aws/dynamodb-stream.ts
145
+ import { array as array3, object as object4, optional, picklist, transform as transform7, unknown as unknown3 } from "valibot";
146
+ var dynamoDbStream = (table) => {
147
+ const marshall = () => transform7(unknown3(), (value) => table.unmarshall(value));
148
+ return transform7(
149
+ object4(
150
+ {
151
+ Records: array3(
152
+ object4({
153
+ eventName: picklist(["MODIFY", "INSERT", "REMOVE"]),
154
+ dynamodb: object4({
155
+ Keys: marshall(),
156
+ OldImage: optional(marshall()),
157
+ NewImage: optional(marshall())
158
+ })
159
+ })
160
+ )
161
+ },
162
+ "Invalid DynamoDB Stream input"
163
+ ),
164
+ (input) => {
165
+ return input.Records.map((record) => {
166
+ const item = record;
167
+ return {
168
+ event: record.eventName,
169
+ keys: item.dynamodb.Keys,
170
+ old: item.dynamodb.OldImage,
171
+ new: item.dynamodb.NewImage
172
+ };
173
+ });
174
+ }
175
+ );
73
176
  };
74
177
 
75
- // src/types/array.ts
76
- import { refine as refine2 } from "superstruct";
77
- function unique(struct2, compare = (a, b) => a === b) {
78
- return refine2(struct2, "unique", (value) => {
79
- for (const x in value) {
80
- for (const y in value) {
81
- if (x !== y && compare(value[x], value[y])) {
82
- return `Expected a ${struct2.type} with unique values, but received "${value}"`;
178
+ // src/validation/positive.ts
179
+ import { ZERO, gt } from "@awsless/big-float";
180
+ import { getOutput as getOutput2, getPipeIssues as getPipeIssues2 } from "valibot";
181
+ function positive(error) {
182
+ return (input) => {
183
+ return gt(input, ZERO) ? getOutput2(input) : getPipeIssues2("positive", error ?? "Invalid positive number", input);
184
+ };
185
+ }
186
+
187
+ // src/validation/precision.ts
188
+ import { BigFloat as BigFloat3 } from "@awsless/big-float";
189
+ import { getOutput as getOutput3, getPipeIssues as getPipeIssues3 } from "valibot";
190
+ function precision(decimals, error) {
191
+ return (input) => {
192
+ const big = new BigFloat3(input.toString());
193
+ return -big.exponent <= decimals ? getOutput3(input) : getPipeIssues3("precision", error ?? `Invalid ${decimals} precision number`, input);
194
+ };
195
+ }
196
+
197
+ // src/validation/unique.ts
198
+ import { getOutput as getOutput4, getPipeIssues as getPipeIssues4 } from "valibot";
199
+ function unique(compare = (a, b) => a === b, error) {
200
+ return (input) => {
201
+ for (const x in input) {
202
+ for (const y in input) {
203
+ if (x !== y && compare(input[x], input[y])) {
204
+ return getPipeIssues4("unique", error ?? "None unique array", input);
83
205
  }
84
206
  }
85
207
  }
86
- return true;
87
- });
208
+ return getOutput4(input);
209
+ };
88
210
  }
89
-
90
- // src/index.ts
91
- import {
92
- coerce as coerce5,
93
- defaulted,
94
- trimmed,
95
- empty,
96
- max,
97
- min,
98
- nonempty,
99
- pattern,
100
- size,
101
- refine as refine3,
102
- any,
103
- array,
104
- bigint as bigint2,
105
- boolean,
106
- enums,
107
- func,
108
- instance,
109
- integer,
110
- intersection,
111
- literal,
112
- map,
113
- never,
114
- nullable,
115
- number as number2,
116
- object as object2,
117
- optional,
118
- record,
119
- regexp,
120
- set,
121
- string as string5,
122
- tuple,
123
- type,
124
- union as union2,
125
- unknown,
126
- assign,
127
- define as define3,
128
- deprecated,
129
- dynamic,
130
- lazy,
131
- omit,
132
- partial,
133
- pick,
134
- struct,
135
- assert,
136
- create,
137
- mask,
138
- is,
139
- Struct as Struct7,
140
- StructError
141
- } from "superstruct";
142
211
  export {
143
- Struct7 as Struct,
144
- StructError,
145
- any,
146
- array,
147
- assert,
148
- assign,
149
212
  bigfloat,
150
- bigint2 as bigint,
151
- boolean,
152
- coerce5 as coerce,
153
- create,
154
213
  date,
155
- defaulted,
156
- define3 as define,
157
- deprecated,
158
- dynamic,
159
- empty,
160
- enums,
161
- func,
162
- instance,
163
- integer,
164
- intersection,
165
- is,
214
+ dynamoDbStream,
166
215
  json,
167
- lazy,
168
- literal,
169
- lowercase,
170
- map,
171
- mask,
172
- max,
173
- min,
174
- never,
175
- nonempty,
176
- nullable,
177
- number2 as number,
178
- object2 as object,
179
- omit,
180
- optional,
181
- partial,
182
- pattern,
183
- pick,
184
216
  positive,
185
217
  precision,
186
- record,
187
- refine3 as refine,
188
- regexp,
189
- set,
190
- size,
191
- string5 as string,
192
- struct,
193
- trimmed,
194
- tuple,
195
- type,
196
- union2 as union,
218
+ snsTopic,
219
+ sqsQueue,
197
220
  unique,
198
- unknown,
199
- uppercase,
200
221
  uuid
201
222
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsless/validate",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -24,13 +24,15 @@
24
24
  }
25
25
  },
26
26
  "peerDependencies": {
27
- "@awsless/big-float": "^0.0.3"
27
+ "@awsless/dynamodb": "^0.0.40",
28
+ "@awsless/big-float": "^0.0.4"
28
29
  },
29
30
  "devDependencies": {
30
- "@awsless/big-float": "^0.0.3"
31
+ "@awsless/big-float": "^0.0.4",
32
+ "@awsless/dynamodb": "^0.0.40"
31
33
  },
32
34
  "dependencies": {
33
- "superstruct": "^1.0.3"
35
+ "valibot": "^0.20.1"
34
36
  },
35
37
  "scripts": {
36
38
  "test": "pnpm code test",