@sanity/validation 2.26.0 → 2.26.1-purple-unicorn.560
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/dts → lib/dts/src}/Rule.d.ts +3 -3
- package/lib/dts/src/Rule.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/ValidationError.d.ts +3 -3
- package/lib/dts/src/ValidationError.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/index.d.ts +12 -12
- package/lib/dts/src/index.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/inferFromSchema.d.ts +3 -3
- package/lib/dts/src/inferFromSchema.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/inferFromSchemaType.d.ts +4 -4
- package/lib/dts/src/inferFromSchemaType.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/util/convertToValidationMarker.d.ts +5 -5
- package/lib/dts/src/util/convertToValidationMarker.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/util/deepEquals.d.ts +5 -5
- package/lib/dts/src/util/deepEquals.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/util/escapeRegex.d.ts +2 -2
- package/lib/dts/src/util/escapeRegex.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/util/normalizeValidationRules.d.ts +5 -5
- package/lib/dts/src/util/normalizeValidationRules.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/util/pathToString.d.ts +2 -2
- package/lib/dts/src/util/pathToString.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/util/typeString.d.ts +1 -1
- package/lib/dts/src/util/typeString.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validateDocument.d.ts +22 -21
- package/lib/dts/src/validateDocument.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/arrayValidator.d.ts +3 -3
- package/lib/dts/src/validators/arrayValidator.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/booleanValidator.d.ts +3 -3
- package/lib/dts/src/validators/booleanValidator.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/dateValidator.d.ts +4 -4
- package/lib/dts/src/validators/dateValidator.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/genericValidator.d.ts +3 -3
- package/lib/dts/src/validators/genericValidator.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/numberValidator.d.ts +3 -3
- package/lib/dts/src/validators/numberValidator.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/objectValidator.d.ts +3 -3
- package/lib/dts/src/validators/objectValidator.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/slugValidator.d.ts +8 -8
- package/lib/dts/src/validators/slugValidator.d.ts.map +1 -0
- package/{dist/dts → lib/dts/src}/validators/stringValidator.d.ts +3 -3
- package/lib/dts/src/validators/stringValidator.d.ts.map +1 -0
- package/lib/index.cjs +1318 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.js +1308 -48
- package/lib/index.js.map +1 -0
- package/package.json +22 -11
- package/src/Rule.ts +6 -2
- package/src/inferFromSchema.ts +7 -1
- package/src/util/convertToValidationMarker.ts +5 -3
- package/src/validateDocument.test.ts +22 -23
- package/src/validateDocument.ts +3 -1
- package/src/validators/dateValidator.ts +2 -2
- package/src/validators/slugValidator.ts +3 -3
- package/src/validators/stringValidator.ts +2 -1
- package/dist/dts/Rule.d.ts.map +0 -1
- package/dist/dts/ValidationError.d.ts.map +0 -1
- package/dist/dts/getClient.d.ts +0 -3
- package/dist/dts/getClient.d.ts.map +0 -1
- package/dist/dts/index.d.ts.map +0 -1
- package/dist/dts/inferFromSchema.d.ts.map +0 -1
- package/dist/dts/inferFromSchemaType.d.ts.map +0 -1
- package/dist/dts/util/convertToValidationMarker.d.ts.map +0 -1
- package/dist/dts/util/deepEquals.d.ts.map +0 -1
- package/dist/dts/util/escapeRegex.d.ts.map +0 -1
- package/dist/dts/util/normalizeValidationRules.d.ts.map +0 -1
- package/dist/dts/util/normalizeValidationRules.test.d.ts +0 -2
- package/dist/dts/util/normalizeValidationRules.test.d.ts.map +0 -1
- package/dist/dts/util/pathToString.d.ts.map +0 -1
- package/dist/dts/util/typeString.d.ts.map +0 -1
- package/dist/dts/util/typeString.test.d.ts +0 -2
- package/dist/dts/util/typeString.test.d.ts.map +0 -1
- package/dist/dts/validateDocument.d.ts.map +0 -1
- package/dist/dts/validateDocument.test.d.ts +0 -2
- package/dist/dts/validateDocument.test.d.ts.map +0 -1
- package/dist/dts/validators/arrayValidator.d.ts.map +0 -1
- package/dist/dts/validators/booleanValidator.d.ts.map +0 -1
- package/dist/dts/validators/dateValidator.d.ts.map +0 -1
- package/dist/dts/validators/genericValidator.d.ts.map +0 -1
- package/dist/dts/validators/numberValidator.d.ts.map +0 -1
- package/dist/dts/validators/objectValidator.d.ts.map +0 -1
- package/dist/dts/validators/slugValidator.d.ts.map +0 -1
- package/dist/dts/validators/stringValidator.d.ts.map +0 -1
- package/jest.config.js +0 -5
- package/lib/Rule.js +0 -491
- package/lib/ValidationError.js +0 -39
- package/lib/getClient.js +0 -20
- package/lib/inferFromSchema.js +0 -22
- package/lib/inferFromSchemaType.js +0 -86
- package/lib/util/convertToValidationMarker.js +0 -65
- package/lib/util/deepEquals.js +0 -87
- package/lib/util/escapeRegex.js +0 -14
- package/lib/util/normalizeValidationRules.js +0 -115
- package/lib/util/pathToString.js +0 -30
- package/lib/util/typeString.js +0 -29
- package/lib/validateDocument.js +0 -231
- package/lib/validators/arrayValidator.js +0 -123
- package/lib/validators/booleanValidator.js +0 -29
- package/lib/validators/dateValidator.js +0 -120
- package/lib/validators/genericValidator.js +0 -135
- package/lib/validators/numberValidator.js +0 -70
- package/lib/validators/objectValidator.js +0 -93
- package/lib/validators/slugValidator.js +0 -133
- package/lib/validators/stringValidator.js +0 -131
- package/src/getClient.ts +0 -13
- package/tsconfig.json +0 -10
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {SlugIsUniqueValidator, Path, CustomValidator, isKeyedObject} from '@sanity/types'
|
|
2
2
|
import {memoize} from 'lodash'
|
|
3
|
-
import getClient from '../getClient'
|
|
3
|
+
// import getClient from '../getClient'
|
|
4
4
|
|
|
5
5
|
const memoizedWarnOnArraySlug = memoize(warnOnArraySlug)
|
|
6
6
|
|
|
@@ -23,7 +23,7 @@ function serializePath(path: Path): string {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
|
|
26
|
-
const {document, path, type} = context
|
|
26
|
+
const {client, document, path, type} = context
|
|
27
27
|
const schemaOptions = type?.options as {disableArrayWarning?: boolean} | undefined
|
|
28
28
|
|
|
29
29
|
if (!document) {
|
|
@@ -48,7 +48,7 @@ const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
|
|
|
48
48
|
`${atPath} == $slug`,
|
|
49
49
|
].join(' && ')
|
|
50
50
|
|
|
51
|
-
return
|
|
51
|
+
return client.fetch<boolean>(
|
|
52
52
|
`!defined(*[${constraints}][0]._id)`,
|
|
53
53
|
{
|
|
54
54
|
docType,
|
|
@@ -2,7 +2,8 @@ import {Validators} from '@sanity/types'
|
|
|
2
2
|
import genericValidator from './genericValidator'
|
|
3
3
|
|
|
4
4
|
const DUMMY_ORIGIN = 'http://sanity'
|
|
5
|
-
const emailRegex =
|
|
5
|
+
const emailRegex =
|
|
6
|
+
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
6
7
|
const isRelativeUrl = (url: string) => /^\.*\//.test(url)
|
|
7
8
|
|
|
8
9
|
const stringValidators: Validators = {
|
package/dist/dts/Rule.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Rule.d.ts","sourceRoot":"","sources":["../../src/Rule.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,SAAS,EAUV,MAAM,eAAe,CAAA;AA0DtB,QAAA,MAAM,IAAI,EAAE,SAkWX,CAAA;AAED,eAAe,IAAI,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationError.d.ts","sourceRoot":"","sources":["../../src/ValidationError.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,oBAAoB,EACrB,MAAM,eAAe,CAAA;AAGtB,QAAA,MAAM,eAAe,EAAE,oBAoBtB,CAAA;AAED,eAAe,eAAe,CAAA"}
|
package/dist/dts/getClient.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getClient.d.ts","sourceRoot":"","sources":["../../src/getClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAA;AAIhD,MAAM,CAAC,OAAO,UAAU,SAAS,IAAI,YAAY,CAQhD"}
|
package/dist/dts/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,QAAQ,CAAA;AAC9B,OAAO,gBAAgB,MAAM,oBAAoB,CAAA;AACjD,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAC/C,OAAO,mBAAmB,MAAM,uBAAuB,CAAA;;;;;;;AAEvD,wBAAwF;AACxF,OAAO,EAAC,SAAS,IAAI,IAAI,EAAE,gBAAgB,EAAE,eAAe,EAAE,mBAAmB,EAAC,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"inferFromSchema.d.ts","sourceRoot":"","sources":["../../src/inferFromSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,eAAe,CAAA;AAIpC,iBAAS,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAM/C;AAED,eAAe,eAAe,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"inferFromSchemaType.d.ts","sourceRoot":"","sources":["../../src/inferFromSchemaType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,eAAe,CAAA;AAoChD,iBAAS,mBAAmB,CAC1B,OAAO,EAAE,UAAU,EAEnB,OAAO,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,GAAG,CAAC,UAAU,CAAC,GACzB,UAAU,CAAA;AAEb,iBAAS,mBAAmB,CAAC,OAAO,EAAE,UAAU,GAAG,UAAU,CAAA;AAM7D,eAAe,mBAAmB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"convertToValidationMarker.d.ts","sourceRoot":"","sources":["../../../src/util/convertToValidationMarker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAE,eAAe,EAAE,iBAAiB,EAAC,MAAM,eAAe,CAAA;AAIlF,aAAK,mBAAmB,GAAG,IAAI,CAAC,eAAe,EAAE,SAAS,CAAC,GAAG,OAAO,CAAC,eAAe,CAAC,CAAA;AAEtF,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAE1D;AAED,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAC/C,eAAe,EACX,IAAI,GACJ,IAAI,EAAE,GACN,MAAM,GACN,MAAM,EAAE,GACR,eAAe,GACf,eAAe,EAAE,GACjB,mBAAmB,GACnB,mBAAmB,EAAE,EACzB,KAAK,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,EAC/C,OAAO,EAAE,iBAAiB,GACzB,gBAAgB,EAAE,CA2DpB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deepEquals.d.ts","sourceRoot":"","sources":["../../../src/util/deepEquals.ts"],"names":[],"mappings":"AAAA;;;IAGI;AAOJ,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAkElE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"escapeRegex.d.ts","sourceRoot":"","sources":["../../../src/util/escapeRegex.ts"],"names":[],"mappings":"iCACwB,MAAM,KAAG,MAAM;AAAvC,wBAGC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeValidationRules.d.ts","sourceRoot":"","sources":["../../../src/util/normalizeValidationRules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,IAAI,EAAqB,MAAM,eAAe,CAAA;AA6ElE;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,wBAAwB,CAAC,OAAO,EAAE,UAAU,GAAG,SAAS,GAAG,IAAI,EAAE,CAqCxF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeValidationRules.test.d.ts","sourceRoot":"","sources":["../../../src/util/normalizeValidationRules.test.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pathToString.d.ts","sourceRoot":"","sources":["../../../src/util/pathToString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAgB,MAAM,eAAe,CAAA;AAEjD,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,IAAI,GAAE,IAAI,GAAG,SAAc,GAAG,MAAM,CAkBxE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"typeString.d.ts","sourceRoot":"","sources":["../../../src/util/typeString.ts"],"names":[],"mappings":"AAaA,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CASvD"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"typeString.test.d.ts","sourceRoot":"","sources":["../../../src/util/typeString.test.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validateDocument.d.ts","sourceRoot":"","sources":["../../src/validateDocument.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,MAAM,EACN,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EAMjB,MAAM,eAAe,CAAA;AAYtB;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,UAAU,EAAE,GACvB,UAAU,GAAG,SAAS,CAiBxB;AAED,wBAA8B,gBAAgB,CAC5C,GAAG,EAAE,cAAc,EACnB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,IAAI,CAAC,iBAAiB,EAAE,mBAAmB,CAAC,GACrD,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA2B7B;AAED;;;;;;GAMG;AACH,aAAK,iBAAiB,CAAC,CAAC,IAAI;KACzB,CAAC,IAAI,MAAM,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS;CAC5F,CAAA;AAED,aAAK,mBAAmB,GAAG;IACzB,KAAK,EAAE,OAAO,CAAA;CACf,GAAG,iBAAiB,CAAC,iBAAiB,CAAC,CAAA;AAExC,wBAAsB,YAAY,CAAC,EACjC,KAAK,EACL,IAAI,EACJ,IAAS,EACT,MAAM,EACN,GAAG,aAAa,EACjB,EAAE,mBAAmB,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA4HnD"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validateDocument.test.d.ts","sourceRoot":"","sources":["../../src/validateDocument.test.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"arrayValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/arrayValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,UAAU,EAAC,MAAM,eAAe,CAAA;AAK3D,QAAA,MAAM,eAAe,EAAE,UA4FtB,CAAA;AAED,eAAe,eAAe,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"booleanValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/booleanValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,eAAe,CAAA;AAGxC,QAAA,MAAM,iBAAiB,EAAE,UAUxB,CAAA;AAED,eAAe,iBAAiB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dateValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/dateValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,eAAe,CAAA;AAIxC,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAErE;AA8CD,QAAA,MAAM,cAAc,EAAE,UAsDrB,CAAA;AAED,eAAe,cAAc,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"genericValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/genericValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,UAAU,EAAC,MAAM,eAAe,CAAA;AA+B1D,QAAA,MAAM,iBAAiB,EAAE,UAmFxB,CAAA;AAED,eAAe,iBAAiB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"numberValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/numberValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,eAAe,CAAA;AAKxC,QAAA,MAAM,gBAAgB,EAAE,UA0DvB,CAAA;AAED,eAAe,gBAAgB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"objectValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/objectValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAc,MAAM,eAAe,CAAA;AAKrD,QAAA,MAAM,gBAAgB,EAAE,UAwDvB,CAAA;AAED,eAAe,gBAAgB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"slugValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/slugValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,eAAe,EAAgB,MAAM,eAAe,CAAA;AA0EzF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,EAAE,eAsB3B,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stringValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/stringValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,eAAe,CAAA;AAOxC,QAAA,MAAM,gBAAgB,EAAE,UA6GvB,CAAA;AAED,eAAe,gBAAgB,CAAA"}
|
package/jest.config.js
DELETED
package/lib/Rule.js
DELETED
|
@@ -1,491 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _get2 = _interopRequireDefault(require("lodash/get"));
|
|
9
|
-
|
|
10
|
-
var _cloneDeep2 = _interopRequireDefault(require("lodash/cloneDeep"));
|
|
11
|
-
|
|
12
|
-
var _ValidationError = _interopRequireDefault(require("./ValidationError"));
|
|
13
|
-
|
|
14
|
-
var _escapeRegex = _interopRequireDefault(require("./util/escapeRegex"));
|
|
15
|
-
|
|
16
|
-
var _convertToValidationMarker = _interopRequireDefault(require("./util/convertToValidationMarker"));
|
|
17
|
-
|
|
18
|
-
var _pathToString = _interopRequireDefault(require("./util/pathToString"));
|
|
19
|
-
|
|
20
|
-
var _genericValidator = _interopRequireDefault(require("./validators/genericValidator"));
|
|
21
|
-
|
|
22
|
-
var _booleanValidator = _interopRequireDefault(require("./validators/booleanValidator"));
|
|
23
|
-
|
|
24
|
-
var _numberValidator = _interopRequireDefault(require("./validators/numberValidator"));
|
|
25
|
-
|
|
26
|
-
var _stringValidator = _interopRequireDefault(require("./validators/stringValidator"));
|
|
27
|
-
|
|
28
|
-
var _arrayValidator = _interopRequireDefault(require("./validators/arrayValidator"));
|
|
29
|
-
|
|
30
|
-
var _objectValidator = _interopRequireDefault(require("./validators/objectValidator"));
|
|
31
|
-
|
|
32
|
-
var _dateValidator = _interopRequireDefault(require("./validators/dateValidator"));
|
|
33
|
-
|
|
34
|
-
var _class, _temp;
|
|
35
|
-
|
|
36
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
37
|
-
|
|
38
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
39
|
-
|
|
40
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
41
|
-
|
|
42
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
43
|
-
|
|
44
|
-
var typeValidators = {
|
|
45
|
-
Boolean: _booleanValidator.default,
|
|
46
|
-
Number: _numberValidator.default,
|
|
47
|
-
String: _stringValidator.default,
|
|
48
|
-
Array: _arrayValidator.default,
|
|
49
|
-
Object: _objectValidator.default,
|
|
50
|
-
Date: _dateValidator.default
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
var getBaseType = type => {
|
|
54
|
-
return type && type.type ? getBaseType(type.type) : type;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
var isFieldRef = constraint => {
|
|
58
|
-
if (typeof constraint !== 'object' || !constraint) return false;
|
|
59
|
-
return constraint.type === Rule.FIELD_REF;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
var EMPTY_ARRAY = [];
|
|
63
|
-
var FIELD_REF = Symbol('FIELD_REF');
|
|
64
|
-
var ruleConstraintTypes = ['Array', 'Boolean', 'Date', 'Number', 'Object', 'String']; // Note: `RuleClass` and `Rule` are split to fit the current `@sanity/types`
|
|
65
|
-
// setup. Classes are a bit weird in the `@sanity/types` package because classes
|
|
66
|
-
// create an actual javascript class while simultaneously creating a type
|
|
67
|
-
// definition.
|
|
68
|
-
//
|
|
69
|
-
// This implicitly creates two types:
|
|
70
|
-
// 1. the instance type — `Rule` and
|
|
71
|
-
// 2. the static/class type - `RuleClass`
|
|
72
|
-
//
|
|
73
|
-
// The `RuleClass` type contains the static methods and the `Rule` instance
|
|
74
|
-
// contains the instance methods.
|
|
75
|
-
//
|
|
76
|
-
// This package exports the RuleClass as a value without implicitly exporting
|
|
77
|
-
// an instance definition. This should help reminder downstream users to import
|
|
78
|
-
// from the `@sanity/types` package.
|
|
79
|
-
|
|
80
|
-
var Rule = (_temp = _class = class Rule {
|
|
81
|
-
constructor(typeDef) {
|
|
82
|
-
_defineProperty(this, "_type", undefined);
|
|
83
|
-
|
|
84
|
-
_defineProperty(this, "_level", undefined);
|
|
85
|
-
|
|
86
|
-
_defineProperty(this, "_required", undefined);
|
|
87
|
-
|
|
88
|
-
_defineProperty(this, "_typeDef", undefined);
|
|
89
|
-
|
|
90
|
-
_defineProperty(this, "_message", undefined);
|
|
91
|
-
|
|
92
|
-
_defineProperty(this, "_rules", []);
|
|
93
|
-
|
|
94
|
-
_defineProperty(this, "_fieldRules", undefined);
|
|
95
|
-
|
|
96
|
-
_defineProperty(this, "valueOfField", Rule.valueOfField.bind(Rule));
|
|
97
|
-
|
|
98
|
-
this._typeDef = typeDef;
|
|
99
|
-
this.reset();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
_mergeRequired(next) {
|
|
103
|
-
if (this._required === 'required' || next._required === 'required') return 'required';
|
|
104
|
-
if (this._required === 'optional' || next._required === 'optional') return 'optional';
|
|
105
|
-
return undefined;
|
|
106
|
-
} // Alias to static method, since we often have access to an _instance_ of a rule but not the actual Rule class
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
error(message) {
|
|
110
|
-
var rule = this.clone();
|
|
111
|
-
rule._level = 'error';
|
|
112
|
-
rule._message = message || undefined;
|
|
113
|
-
return rule;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
warning(message) {
|
|
117
|
-
var rule = this.clone();
|
|
118
|
-
rule._level = 'warning';
|
|
119
|
-
rule._message = message || undefined;
|
|
120
|
-
return rule;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
info(message) {
|
|
124
|
-
var rule = this.clone();
|
|
125
|
-
rule._level = 'info';
|
|
126
|
-
rule._message = message || undefined;
|
|
127
|
-
return rule;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
reset() {
|
|
131
|
-
this._type = this._type || undefined;
|
|
132
|
-
this._rules = (this._rules || []).filter(rule => rule.flag === 'type');
|
|
133
|
-
this._message = undefined;
|
|
134
|
-
this._required = undefined;
|
|
135
|
-
this._level = 'error';
|
|
136
|
-
this._fieldRules = undefined;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
isRequired() {
|
|
141
|
-
return this._required === 'required';
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
clone() {
|
|
145
|
-
var rule = new Rule();
|
|
146
|
-
rule._type = this._type;
|
|
147
|
-
rule._message = this._message;
|
|
148
|
-
rule._required = this._required;
|
|
149
|
-
rule._rules = (0, _cloneDeep2.default)(this._rules);
|
|
150
|
-
rule._level = this._level;
|
|
151
|
-
rule._fieldRules = this._fieldRules;
|
|
152
|
-
rule._typeDef = this._typeDef;
|
|
153
|
-
return rule;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
cloneWithRules(rules) {
|
|
157
|
-
var rule = this.clone();
|
|
158
|
-
var newRules = new Set();
|
|
159
|
-
rules.forEach(curr => {
|
|
160
|
-
if (curr.flag === 'type') {
|
|
161
|
-
rule._type = curr.constraint;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
newRules.add(curr.flag);
|
|
165
|
-
});
|
|
166
|
-
rule._rules = rule._rules.filter(curr => {
|
|
167
|
-
var disallowDuplicate = ['type', 'uri', 'email'].includes(curr.flag);
|
|
168
|
-
var isDuplicate = newRules.has(curr.flag);
|
|
169
|
-
return !(disallowDuplicate && isDuplicate);
|
|
170
|
-
}).concat(rules);
|
|
171
|
-
return rule;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
merge(rule) {
|
|
175
|
-
if (this._type && rule._type && this._type !== rule._type) {
|
|
176
|
-
throw new Error('merge() failed: conflicting types');
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
var newRule = this.cloneWithRules(rule._rules);
|
|
180
|
-
newRule._type = this._type || rule._type;
|
|
181
|
-
newRule._message = this._message || rule._message;
|
|
182
|
-
newRule._required = this._mergeRequired(rule);
|
|
183
|
-
newRule._level = this._level === 'error' ? rule._level : this._level;
|
|
184
|
-
return newRule;
|
|
185
|
-
} // Validation flag setters
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
type(targetType) {
|
|
189
|
-
var type = "".concat(targetType.slice(0, 1).toUpperCase()).concat(targetType.slice(1));
|
|
190
|
-
|
|
191
|
-
if (!ruleConstraintTypes.includes(type)) {
|
|
192
|
-
throw new Error("Unknown type \"".concat(targetType, "\""));
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
var rule = this.cloneWithRules([{
|
|
196
|
-
flag: 'type',
|
|
197
|
-
constraint: type
|
|
198
|
-
}]);
|
|
199
|
-
rule._type = type;
|
|
200
|
-
return rule;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
all(children) {
|
|
204
|
-
return this.cloneWithRules([{
|
|
205
|
-
flag: 'all',
|
|
206
|
-
constraint: children
|
|
207
|
-
}]);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
either(children) {
|
|
211
|
-
return this.cloneWithRules([{
|
|
212
|
-
flag: 'either',
|
|
213
|
-
constraint: children
|
|
214
|
-
}]);
|
|
215
|
-
} // Shared rules
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
optional() {
|
|
219
|
-
var rule = this.cloneWithRules([{
|
|
220
|
-
flag: 'presence',
|
|
221
|
-
constraint: 'optional'
|
|
222
|
-
}]);
|
|
223
|
-
rule._required = 'optional';
|
|
224
|
-
return rule;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
required() {
|
|
228
|
-
var rule = this.cloneWithRules([{
|
|
229
|
-
flag: 'presence',
|
|
230
|
-
constraint: 'required'
|
|
231
|
-
}]);
|
|
232
|
-
rule._required = 'required';
|
|
233
|
-
return rule;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
custom(fn) {
|
|
237
|
-
return this.cloneWithRules([{
|
|
238
|
-
flag: 'custom',
|
|
239
|
-
constraint: fn
|
|
240
|
-
}]);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* @deprecated use `Rule.custom` instead
|
|
244
|
-
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
block(fn) {
|
|
248
|
-
return this.cloneWithRules([{
|
|
249
|
-
flag: 'custom',
|
|
250
|
-
constraint: fn
|
|
251
|
-
}]);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
min(len) {
|
|
255
|
-
return this.cloneWithRules([{
|
|
256
|
-
flag: 'min',
|
|
257
|
-
constraint: len
|
|
258
|
-
}]);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
max(len) {
|
|
262
|
-
return this.cloneWithRules([{
|
|
263
|
-
flag: 'max',
|
|
264
|
-
constraint: len
|
|
265
|
-
}]);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
length(len) {
|
|
269
|
-
return this.cloneWithRules([{
|
|
270
|
-
flag: 'length',
|
|
271
|
-
constraint: len
|
|
272
|
-
}]);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
valid(value) {
|
|
276
|
-
var values = Array.isArray(value) ? value : [value];
|
|
277
|
-
return this.cloneWithRules([{
|
|
278
|
-
flag: 'valid',
|
|
279
|
-
constraint: values
|
|
280
|
-
}]);
|
|
281
|
-
} // Numbers only
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
integer() {
|
|
285
|
-
return this.cloneWithRules([{
|
|
286
|
-
flag: 'integer'
|
|
287
|
-
}]);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
precision(limit) {
|
|
291
|
-
return this.cloneWithRules([{
|
|
292
|
-
flag: 'precision',
|
|
293
|
-
constraint: limit
|
|
294
|
-
}]);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
positive() {
|
|
298
|
-
return this.cloneWithRules([{
|
|
299
|
-
flag: 'min',
|
|
300
|
-
constraint: 0
|
|
301
|
-
}]);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
negative() {
|
|
305
|
-
return this.cloneWithRules([{
|
|
306
|
-
flag: 'lessThan',
|
|
307
|
-
constraint: 0
|
|
308
|
-
}]);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
greaterThan(num) {
|
|
312
|
-
return this.cloneWithRules([{
|
|
313
|
-
flag: 'greaterThan',
|
|
314
|
-
constraint: num
|
|
315
|
-
}]);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
lessThan(num) {
|
|
319
|
-
return this.cloneWithRules([{
|
|
320
|
-
flag: 'lessThan',
|
|
321
|
-
constraint: num
|
|
322
|
-
}]);
|
|
323
|
-
} // String only
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
uppercase() {
|
|
327
|
-
return this.cloneWithRules([{
|
|
328
|
-
flag: 'stringCasing',
|
|
329
|
-
constraint: 'uppercase'
|
|
330
|
-
}]);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
lowercase() {
|
|
334
|
-
return this.cloneWithRules([{
|
|
335
|
-
flag: 'stringCasing',
|
|
336
|
-
constraint: 'lowercase'
|
|
337
|
-
}]);
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
regex(pattern, a, b) {
|
|
341
|
-
var _a$name, _a$invert;
|
|
342
|
-
|
|
343
|
-
var name = typeof a === 'string' ? a : (_a$name = a === null || a === void 0 ? void 0 : a.name) !== null && _a$name !== void 0 ? _a$name : b === null || b === void 0 ? void 0 : b.name;
|
|
344
|
-
var invert = typeof a === 'string' ? false : (_a$invert = a === null || a === void 0 ? void 0 : a.invert) !== null && _a$invert !== void 0 ? _a$invert : b === null || b === void 0 ? void 0 : b.invert;
|
|
345
|
-
var constraint = {
|
|
346
|
-
pattern,
|
|
347
|
-
name,
|
|
348
|
-
invert: invert || false
|
|
349
|
-
};
|
|
350
|
-
return this.cloneWithRules([{
|
|
351
|
-
flag: 'regex',
|
|
352
|
-
constraint
|
|
353
|
-
}]);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
email() {
|
|
357
|
-
return this.cloneWithRules([{
|
|
358
|
-
flag: 'email'
|
|
359
|
-
}]);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
uri(opts) {
|
|
363
|
-
var optsScheme = (opts === null || opts === void 0 ? void 0 : opts.scheme) || ['http', 'https'];
|
|
364
|
-
var schemes = Array.isArray(optsScheme) ? optsScheme : [optsScheme];
|
|
365
|
-
|
|
366
|
-
if (!schemes.length) {
|
|
367
|
-
throw new Error('scheme must have at least 1 scheme specified');
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
var constraint = {
|
|
371
|
-
options: {
|
|
372
|
-
scheme: schemes.map(scheme => {
|
|
373
|
-
if (!(scheme instanceof RegExp) && typeof scheme !== 'string') {
|
|
374
|
-
throw new Error('scheme must be a RegExp or a String');
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
return typeof scheme === 'string' ? new RegExp("^".concat((0, _escapeRegex.default)(scheme), "$")) : scheme;
|
|
378
|
-
}),
|
|
379
|
-
allowRelative: (opts === null || opts === void 0 ? void 0 : opts.allowRelative) || false,
|
|
380
|
-
relativeOnly: (opts === null || opts === void 0 ? void 0 : opts.relativeOnly) || false,
|
|
381
|
-
allowCredentials: (opts === null || opts === void 0 ? void 0 : opts.allowCredentials) || false
|
|
382
|
-
}
|
|
383
|
-
};
|
|
384
|
-
return this.cloneWithRules([{
|
|
385
|
-
flag: 'uri',
|
|
386
|
-
constraint
|
|
387
|
-
}]);
|
|
388
|
-
} // Array only
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
unique() {
|
|
392
|
-
return this.cloneWithRules([{
|
|
393
|
-
flag: 'unique'
|
|
394
|
-
}]);
|
|
395
|
-
} // Objects only
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
reference() {
|
|
399
|
-
return this.cloneWithRules([{
|
|
400
|
-
flag: 'reference'
|
|
401
|
-
}]);
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
fields(rules) {
|
|
405
|
-
if (this._type !== 'Object') {
|
|
406
|
-
throw new Error('fields() can only be called on an object type');
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
var rule = this.cloneWithRules([]);
|
|
410
|
-
rule._fieldRules = rules;
|
|
411
|
-
return rule;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
assetRequired() {
|
|
415
|
-
var base = getBaseType(this._typeDef);
|
|
416
|
-
var assetType;
|
|
417
|
-
|
|
418
|
-
if (base && ['image', 'file'].includes(base.name)) {
|
|
419
|
-
assetType = base.name === 'image' ? 'Image' : 'File';
|
|
420
|
-
} else {
|
|
421
|
-
assetType = 'Asset';
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
return this.cloneWithRules([{
|
|
425
|
-
flag: 'assetRequired',
|
|
426
|
-
constraint: {
|
|
427
|
-
assetType
|
|
428
|
-
}
|
|
429
|
-
}]);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
validate(value) {
|
|
433
|
-
var _arguments = arguments,
|
|
434
|
-
_this = this;
|
|
435
|
-
|
|
436
|
-
return _asyncToGenerator(function* () {
|
|
437
|
-
var context = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : {};
|
|
438
|
-
var valueIsEmpty = value === null || value === undefined; // Short-circuit on optional, empty fields
|
|
439
|
-
|
|
440
|
-
if (valueIsEmpty && _this._required === 'optional') {
|
|
441
|
-
return EMPTY_ARRAY;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
var rules = // Run only the _custom_ functions if the rule is not set to required or optional
|
|
445
|
-
_this._required === undefined && valueIsEmpty ? _this._rules.filter(curr => curr.flag === 'custom') : _this._rules;
|
|
446
|
-
var validators = _this._type && typeValidators[_this._type] || _genericValidator.default;
|
|
447
|
-
var results = yield Promise.all(rules.map( /*#__PURE__*/function () {
|
|
448
|
-
var _ref = _asyncToGenerator(function* (curr) {
|
|
449
|
-
if (curr.flag === undefined) {
|
|
450
|
-
throw new Error('Invalid rule, did not contain "flag"-property');
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
var validator = validators[curr.flag];
|
|
454
|
-
|
|
455
|
-
if (!validator) {
|
|
456
|
-
var forType = _this._type ? "type \"".concat(_this._type, "\"") : 'rule without declared type';
|
|
457
|
-
throw new Error("Validator for flag \"".concat(curr.flag, "\" not found for ").concat(forType));
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
var specConstraint = 'constraint' in curr ? curr.constraint : null;
|
|
461
|
-
|
|
462
|
-
if (isFieldRef(specConstraint)) {
|
|
463
|
-
specConstraint = (0, _get2.default)(context.parent, specConstraint.path);
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
var result;
|
|
467
|
-
|
|
468
|
-
try {
|
|
469
|
-
result = yield validator(specConstraint, value, _this._message, context);
|
|
470
|
-
} catch (err) {
|
|
471
|
-
var errorFromException = new _ValidationError.default("".concat((0, _pathToString.default)(context.path), ": Exception occurred while validating value: ").concat(err.message));
|
|
472
|
-
return (0, _convertToValidationMarker.default)(errorFromException, 'error', context);
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
return (0, _convertToValidationMarker.default)(result, _this._level, context);
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
return function (_x) {
|
|
479
|
-
return _ref.apply(this, arguments);
|
|
480
|
-
};
|
|
481
|
-
}()));
|
|
482
|
-
return results.flat();
|
|
483
|
-
})();
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
}, _defineProperty(_class, "FIELD_REF", FIELD_REF), _defineProperty(_class, "array", def => new _class(def).type('Array')), _defineProperty(_class, "object", def => new _class(def).type('Object')), _defineProperty(_class, "string", def => new _class(def).type('String')), _defineProperty(_class, "number", def => new _class(def).type('Number')), _defineProperty(_class, "boolean", def => new _class(def).type('Boolean')), _defineProperty(_class, "dateTime", def => new _class(def).type('Date')), _defineProperty(_class, "valueOfField", path => ({
|
|
487
|
-
type: FIELD_REF,
|
|
488
|
-
path
|
|
489
|
-
})), _temp);
|
|
490
|
-
var _default = Rule;
|
|
491
|
-
exports.default = _default;
|