@sanity/validation 3.3.1 → 3.4.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/lib/dts/src/index.d.ts +17 -3
- package/lib/index.cjs.mjs +2 -0
- package/lib/index.esm.js +7 -16
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +7 -16
- package/lib/index.js.map +1 -1
- package/package.json +5 -8
- package/src/validateDocument.test.ts +5 -10
- package/src/validateDocument.ts +3 -29
package/lib/dts/src/index.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import {Observable} from 'rxjs'
|
|
2
2
|
import {RuleClass} from '@sanity/types'
|
|
3
|
-
import type {SanityClient} from '@sanity/client'
|
|
4
3
|
import {SanityDocument} from '@sanity/types'
|
|
5
4
|
import {Schema} from '@sanity/types'
|
|
6
5
|
import {SchemaType} from '@sanity/types'
|
|
7
6
|
import {ValidationContext} from '@sanity/types'
|
|
8
7
|
import {ValidationMarker} from '@sanity/types'
|
|
9
8
|
|
|
9
|
+
/**
|
|
10
|
+
* this is used make optional properties required by replacing optionals with
|
|
11
|
+
* `T[P] | undefined`. this is used to prevent errors in `validateItem` where
|
|
12
|
+
* an option from a previous invocation would be incorrectly passed down.
|
|
13
|
+
*
|
|
14
|
+
* https://medium.com/terria/typescript-transforming-optional-properties-to-required-properties-that-may-be-undefined-7482cb4e1585
|
|
15
|
+
*/
|
|
16
|
+
declare type ExplicitUndefined<T> = {
|
|
17
|
+
[P in keyof Required<T>]: Pick<T, P> extends Required<Pick<T, P>> ? T[P] : T[P] | undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
10
20
|
export declare function inferFromSchema(schema: Schema): Schema
|
|
11
21
|
|
|
12
22
|
export declare function inferFromSchemaType(
|
|
@@ -20,17 +30,21 @@ export declare function inferFromSchemaType(typeDef: SchemaType): SchemaType
|
|
|
20
30
|
export declare const Rule: RuleClass
|
|
21
31
|
|
|
22
32
|
export declare function validateDocument(
|
|
23
|
-
getClient:
|
|
33
|
+
getClient: ValidateItemOptions['getClient'],
|
|
24
34
|
doc: SanityDocument,
|
|
25
35
|
schema: Schema,
|
|
26
36
|
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
27
37
|
): Promise<ValidationMarker[]>
|
|
28
38
|
|
|
29
39
|
export declare function validateDocumentObservable(
|
|
30
|
-
getClient:
|
|
40
|
+
getClient: ValidateItemOptions['getClient'],
|
|
31
41
|
doc: SanityDocument,
|
|
32
42
|
schema: Schema,
|
|
33
43
|
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
34
44
|
): Observable<ValidationMarker[]>
|
|
35
45
|
|
|
46
|
+
declare type ValidateItemOptions = {
|
|
47
|
+
value: unknown
|
|
48
|
+
} & ExplicitUndefined<ValidationContext>
|
|
49
|
+
|
|
36
50
|
export {}
|
package/lib/index.cjs.mjs
CHANGED
|
@@ -5,3 +5,5 @@ export const inferFromSchema = cjs.inferFromSchema;
|
|
|
5
5
|
export const inferFromSchemaType = cjs.inferFromSchemaType;
|
|
6
6
|
export const validateDocument = cjs.validateDocument;
|
|
7
7
|
export const validateDocumentObservable = cjs.validateDocumentObservable;
|
|
8
|
+
|
|
9
|
+
export default cjs.default;
|
package/lib/index.esm.js
CHANGED
|
@@ -520,7 +520,7 @@ const getFormattedDate = function () {
|
|
|
520
520
|
function parseDate(date) {
|
|
521
521
|
let throwOnError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
522
522
|
if (!date) return null;
|
|
523
|
-
if (date === "now") return new Date();
|
|
523
|
+
if (date === "now") return /* @__PURE__ */new Date();
|
|
524
524
|
const parsed = new Date(date);
|
|
525
525
|
const isInvalid = isNaN(parsed.getTime());
|
|
526
526
|
if (isInvalid && throwOnError) {
|
|
@@ -1117,11 +1117,7 @@ function validateDocumentObservable(getClient, doc, schema, context) {
|
|
|
1117
1117
|
console.warn('Schema type for object type "%s" not found, skipping validation', doc._type);
|
|
1118
1118
|
return of(EMPTY_MARKERS);
|
|
1119
1119
|
}
|
|
1120
|
-
const client = getClient({
|
|
1121
|
-
apiVersion: "2021-06-07"
|
|
1122
|
-
});
|
|
1123
1120
|
const validationOptions = {
|
|
1124
|
-
client,
|
|
1125
1121
|
getClient,
|
|
1126
1122
|
schema,
|
|
1127
1123
|
parent: void 0,
|
|
@@ -1131,16 +1127,6 @@ function validateDocumentObservable(getClient, doc, schema, context) {
|
|
|
1131
1127
|
type: documentType,
|
|
1132
1128
|
getDocumentExists: context == null ? void 0 : context.getDocumentExists
|
|
1133
1129
|
};
|
|
1134
|
-
const wrappedClient = client;
|
|
1135
|
-
validationOptions.client = [...Object.keys(client), ...Object.keys(wrappedClient.__proto__)].reduce((acc, key) => {
|
|
1136
|
-
const original = Object.hasOwnProperty.call(client, key) ? wrappedClient[key] : wrappedClient.__proto__[key];
|
|
1137
|
-
return Object.defineProperty(acc, key, {
|
|
1138
|
-
get() {
|
|
1139
|
-
console.warn('`configContext.client` is deprecated and will be removed in the next release! Use `context.getClient({apiVersion: "2021-06-07"})` instead');
|
|
1140
|
-
return original;
|
|
1141
|
-
}
|
|
1142
|
-
});
|
|
1143
|
-
}, {});
|
|
1144
1130
|
return validateItemObservable(validationOptions).pipe(catchError(err => {
|
|
1145
1131
|
console.error(err);
|
|
1146
1132
|
return of([{
|
|
@@ -1171,7 +1157,12 @@ function validateItemObservable(_ref) {
|
|
|
1171
1157
|
const selfIsRequired = rules.some(rule => rule.isRequired());
|
|
1172
1158
|
const shouldRunNestedObjectValidation =
|
|
1173
1159
|
// run nested validation for objects
|
|
1174
|
-
(type == null ? void 0 : type.jsonType) === "object" && (
|
|
1160
|
+
(type == null ? void 0 : type.jsonType) === "object" && (
|
|
1161
|
+
// if the value is truthy
|
|
1162
|
+
!!value ||
|
|
1163
|
+
// or
|
|
1164
|
+
// (the value is null or undefined) and the top-level value is required
|
|
1165
|
+
(value === null || value === void 0) && selfIsRequired);
|
|
1175
1166
|
if (shouldRunNestedObjectValidation) {
|
|
1176
1167
|
const fieldTypes = type.fields.reduce((acc, field) => {
|
|
1177
1168
|
acc[field.name] = field.type;
|