@aeriajs/validation 0.0.141 → 0.0.143

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/validate.js CHANGED
@@ -4,6 +4,9 @@ exports.validator = exports.validateWithRefs = exports.validate = exports.valida
4
4
  const common_1 = require("@aeriajs/common");
5
5
  const types_1 = require("@aeriajs/types");
6
6
  const entrypoint_1 = require("@aeriajs/entrypoint");
7
+ const isValidObjectId = (what) => {
8
+ return /^[0-9a-f]{24}$/.test(what);
9
+ };
7
10
  const getPropertyType = (property) => {
8
11
  if ('type' in property) {
9
12
  if ('format' in property && property.format) {
@@ -68,7 +71,7 @@ const validateProperty = (what, property, options = {}) => {
68
71
  if ('$ref' in property) {
69
72
  switch (typeof what) {
70
73
  case 'string': {
71
- if (/^[0-9a-f]{24}$/.test(what)) {
74
+ if (isValidObjectId(what)) {
72
75
  return types_1.Result.result(what);
73
76
  }
74
77
  return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
@@ -102,6 +105,23 @@ const validateProperty = (what, property, options = {}) => {
102
105
  }
103
106
  if ('type' in property) {
104
107
  switch (property.type) {
108
+ case 'string': {
109
+ if (typeof what !== 'string') {
110
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
111
+ expected: expectedType,
112
+ got: actualType,
113
+ }));
114
+ }
115
+ if ((property.format === 'objectid' && !isValidObjectId(what))
116
+ || (typeof property.minLength === 'number' && property.minLength > what.length)
117
+ || (typeof property.maxLength === 'number' && property.maxLength < what.length)) {
118
+ return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.StringConstraint, {
119
+ expected: 'string',
120
+ got: 'invalid_string',
121
+ }));
122
+ }
123
+ break;
124
+ }
105
125
  case 'integer': {
106
126
  if (!Number.isInteger(what)) {
107
127
  return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.NumericConstraint, {
package/dist/validate.mjs CHANGED
@@ -2,6 +2,9 @@
2
2
  import { getMissingProperties } from "@aeriajs/common";
3
3
  import { Result, ValidationErrorCode, PropertyValidationErrorCode } from "@aeriajs/types";
4
4
  import { getCollection } from "@aeriajs/entrypoint";
5
+ const isValidObjectId = (what) => {
6
+ return /^[0-9a-f]{24}$/.test(what);
7
+ };
5
8
  const getPropertyType = (property) => {
6
9
  if ("type" in property) {
7
10
  if ("format" in property && property.format) {
@@ -60,7 +63,7 @@ export const validateProperty = (what, property, options = {}) => {
60
63
  if ("$ref" in property) {
61
64
  switch (typeof what) {
62
65
  case "string": {
63
- if (/^[0-9a-f]{24}$/.test(what)) {
66
+ if (isValidObjectId(what)) {
64
67
  return Result.result(what);
65
68
  }
66
69
  return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
@@ -94,6 +97,21 @@ export const validateProperty = (what, property, options = {}) => {
94
97
  }
95
98
  if ("type" in property) {
96
99
  switch (property.type) {
100
+ case "string": {
101
+ if (typeof what !== "string") {
102
+ return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
103
+ expected: expectedType,
104
+ got: actualType
105
+ }));
106
+ }
107
+ if (property.format === "objectid" && !isValidObjectId(what) || typeof property.minLength === "number" && property.minLength > what.length || typeof property.maxLength === "number" && property.maxLength < what.length) {
108
+ return Result.error(makePropertyError(PropertyValidationErrorCode.StringConstraint, {
109
+ expected: "string",
110
+ got: "invalid_string"
111
+ }));
112
+ }
113
+ break;
114
+ }
97
115
  case "integer": {
98
116
  if (!Number.isInteger(what)) {
99
117
  return Result.error(makePropertyError(PropertyValidationErrorCode.NumericConstraint, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.141",
3
+ "version": "0.0.143",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -27,9 +27,9 @@
27
27
  "@aeriajs/types": "link:../types"
28
28
  },
29
29
  "peerDependencies": {
30
- "@aeriajs/common": "^0.0.126",
31
- "@aeriajs/entrypoint": "^0.0.129",
32
- "@aeriajs/types": "^0.0.108",
30
+ "@aeriajs/common": "^0.0.128",
31
+ "@aeriajs/entrypoint": "^0.0.131",
32
+ "@aeriajs/types": "^0.0.110",
33
33
  "mongodb": "^6.5.0"
34
34
  },
35
35
  "scripts": {