| 1367 | } |
| 1368 | |
| 1369 | export class ZodNumber extends ZodType<number, ZodNumberDef, number> { |
| 1370 | _parse(input: ParseInput): ParseReturnType<number> { |
| 1371 | if (this._def.coerce) { |
| 1372 | input.data = Number(input.data); |
| 1373 | } |
| 1374 | const parsedType = this._getType(input); |
| 1375 | if (parsedType !== ZodParsedType.number) { |
| 1376 | const ctx = this._getOrReturnCtx(input); |
| 1377 | addIssueToContext(ctx, { |
| 1378 | code: ZodIssueCode.invalid_type, |
| 1379 | expected: ZodParsedType.number, |
| 1380 | received: ctx.parsedType, |
| 1381 | }); |
| 1382 | return INVALID; |
| 1383 | } |
| 1384 | |
| 1385 | let ctx: undefined | ParseContext = undefined; |
| 1386 | const status = new ParseStatus(); |
| 1387 | |
| 1388 | for (const check of this._def.checks) { |
| 1389 | if (check.kind === "int") { |
| 1390 | if (!util.isInteger(input.data)) { |
| 1391 | ctx = this._getOrReturnCtx(input, ctx); |
| 1392 | addIssueToContext(ctx, { |
| 1393 | code: ZodIssueCode.invalid_type, |
| 1394 | expected: "integer", |
| 1395 | received: "float", |
| 1396 | message: check.message, |
| 1397 | }); |
| 1398 | status.dirty(); |
| 1399 | } |
| 1400 | } else if (check.kind === "min") { |
| 1401 | const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value; |
| 1402 | if (tooSmall) { |
| 1403 | ctx = this._getOrReturnCtx(input, ctx); |
| 1404 | addIssueToContext(ctx, { |
| 1405 | code: ZodIssueCode.too_small, |
| 1406 | minimum: check.value, |
| 1407 | type: "number", |
| 1408 | inclusive: check.inclusive, |
| 1409 | exact: false, |
| 1410 | message: check.message, |
| 1411 | }); |
| 1412 | status.dirty(); |
| 1413 | } |
| 1414 | } else if (check.kind === "max") { |
| 1415 | const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value; |
| 1416 | if (tooBig) { |
| 1417 | ctx = this._getOrReturnCtx(input, ctx); |
| 1418 | addIssueToContext(ctx, { |
| 1419 | code: ZodIssueCode.too_big, |
| 1420 | maximum: check.value, |
| 1421 | type: "number", |
| 1422 | inclusive: check.inclusive, |
| 1423 | exact: false, |
| 1424 | message: check.message, |
| 1425 | }); |
| 1426 | status.dirty(); |
nothing calls this directly
no test coverage detected