@bedrockio/yada 1.11.0 → 1.11.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.11.1
2
+
3
+ - Better handling of tuple schemas in `toOpenAi`.
4
+
1
5
  ## 1.11.0
2
6
 
3
7
  - Moved `openai` as `toJSONSchema` option to own method `toOpenAi`.
@@ -15,6 +15,15 @@ function toOpenAi(schema) {
15
15
  } = schema.meta;
16
16
  if (type === 'object') {
17
17
  return schema.required();
18
+ } else if (type === 'array') {
19
+ const {
20
+ schemas
21
+ } = schema.meta;
22
+ if (schemas) {
23
+ return schema.toArray().required();
24
+ } else {
25
+ return schema.required();
26
+ }
18
27
  }
19
28
 
20
29
  // All fields in OpenAI flavor JSON schema must be required,
package/dist/cjs/tuple.js CHANGED
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = _default;
7
+ var _lodash = require("lodash");
7
8
  var _Schema = _interopRequireDefault(require("./Schema"));
9
+ var _array = _interopRequireDefault(require("./array"));
8
10
  var _errors = require("./errors");
9
11
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
12
  class TupleSchema extends _Schema.default {
@@ -77,6 +79,17 @@ class TupleSchema extends _Schema.default {
77
79
  loose: true
78
80
  });
79
81
  }
82
+ toArray() {
83
+ const {
84
+ schemas
85
+ } = this.meta;
86
+ const unique = (0, _lodash.uniqBy)(schemas, s => s.meta.type);
87
+ if (unique.length > 1) {
88
+ return (0, _array.default)(new _Schema.default().allow(unique));
89
+ } else {
90
+ return (0, _array.default)(unique[0]);
91
+ }
92
+ }
80
93
  toString() {
81
94
  return 'tuple';
82
95
  }
@@ -86,6 +99,7 @@ class TupleSchema extends _Schema.default {
86
99
  } = this.meta;
87
100
  return {
88
101
  ...super.toJsonSchema(options),
102
+ items: false,
89
103
  prefixItems: schemas.map(schema => {
90
104
  return schema.toJsonSchema(options);
91
105
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.11.0",
3
+ "version": "1.11.1",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/openai.js CHANGED
@@ -16,6 +16,13 @@ export function toOpenAi(schema) {
16
16
 
17
17
  if (type === 'object') {
18
18
  return schema.required();
19
+ } else if (type === 'array') {
20
+ const { schemas } = schema.meta;
21
+ if (schemas) {
22
+ return schema.toArray().required();
23
+ } else {
24
+ return schema.required();
25
+ }
19
26
  }
20
27
 
21
28
  // All fields in OpenAI flavor JSON schema must be required,
package/src/tuple.js CHANGED
@@ -1,4 +1,7 @@
1
+ import { uniqBy } from 'lodash';
2
+
1
3
  import Schema from './Schema';
4
+ import array from './array';
2
5
  import { ArrayError, ElementError, LocalizedError } from './errors';
3
6
 
4
7
  class TupleSchema extends Schema {
@@ -65,6 +68,16 @@ class TupleSchema extends Schema {
65
68
  return this.clone({ loose: true });
66
69
  }
67
70
 
71
+ toArray() {
72
+ const { schemas } = this.meta;
73
+ const unique = uniqBy(schemas, (s) => s.meta.type);
74
+ if (unique.length > 1) {
75
+ return array(new Schema().allow(unique));
76
+ } else {
77
+ return array(unique[0]);
78
+ }
79
+ }
80
+
68
81
  toString() {
69
82
  return 'tuple';
70
83
  }
@@ -73,6 +86,7 @@ class TupleSchema extends Schema {
73
86
  const { schemas } = this.meta;
74
87
  return {
75
88
  ...super.toJsonSchema(options),
89
+ items: false,
76
90
  prefixItems: schemas.map((schema) => {
77
91
  return schema.toJsonSchema(options);
78
92
  }),
@@ -1 +1 @@
1
- {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../src/openai.js"],"names":[],"mappings":"AAaA,2CA0BC"}
1
+ {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../src/openai.js"],"names":[],"mappings":"AAaA,2CAiCC"}
package/types/tuple.d.ts CHANGED
@@ -11,6 +11,60 @@ declare class TupleSchema extends Schema {
11
11
  constructor(schemas: any);
12
12
  setup(): void;
13
13
  loose(): this;
14
+ toArray(): {
15
+ setup(): void;
16
+ length(length: any): /*elided*/ any;
17
+ min(length: any): /*elided*/ any;
18
+ max(length: any): /*elided*/ any;
19
+ latlng(): /*elided*/ any;
20
+ transform(fn: Function, root?: boolean): /*elided*/ any;
21
+ set(path: string | Array<string>, fn: Function): /*elided*/ any;
22
+ toString(): string;
23
+ toJsonSchema(options: any): any;
24
+ format(name: any, fn: any): /*elided*/ any;
25
+ assertions: any[];
26
+ meta: {};
27
+ required(allow?: boolean): /*elided*/ any;
28
+ default(arg: any): /*elided*/ any;
29
+ custom(fn: Function): /*elided*/ any;
30
+ missing(fn: Function): /*elided*/ any;
31
+ strip(strip: any): /*elided*/ any;
32
+ allow(...set: any[]): /*elided*/ any;
33
+ reject(...set: any[]): /*elided*/ any;
34
+ nullable(allow?: boolean): /*elided*/ any;
35
+ message(message: any): /*elided*/ any;
36
+ tag(tags: any): /*elided*/ any;
37
+ description(description: any): /*elided*/ any;
38
+ options(options: any): /*elided*/ any;
39
+ validate(value: any, options?: {}): Promise<any>;
40
+ clone(meta: any): /*elided*/ any;
41
+ append(schema: any): Schema;
42
+ toOpenApi(options: any): any;
43
+ toOpenAi(): /*elided*/ any;
44
+ toJSON(): any;
45
+ getType(): {
46
+ type: any;
47
+ };
48
+ getFormat(): {
49
+ format: any;
50
+ };
51
+ getDefault(): {
52
+ default?: undefined;
53
+ } | {
54
+ default: any;
55
+ };
56
+ getEnum(options: any): any;
57
+ getTags(options?: {}): any;
58
+ inspect(): string;
59
+ get(): void;
60
+ assertEnum(set: any, allow: any): /*elided*/ any;
61
+ assert(type: any, fn: any): /*elided*/ any;
62
+ pushAssertion(assertion: any): void;
63
+ canSkipAssertion(value: any, assertion: any, options: any): any;
64
+ transformValue(fn: any): /*elided*/ any;
65
+ getSortIndex(type: any): number;
66
+ runAssertion(value: any, assertion: any, options?: {}): Promise<any>;
67
+ };
14
68
  toJsonSchema(options: any): any;
15
69
  }
16
70
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../src/tuple.js"],"names":[],"mappings":"AAkFA;;;;;;GAMG;AACH,6CAJc,MAAM,EAAA,eASnB;mBA9FkB,UAAU;AAG7B;IACE,0BAGC;IAED,cAoDC;IAED,cAEC;IAMD,gCAQC;CACF"}
1
+ {"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../src/tuple.js"],"names":[],"mappings":"AAgGA;;;;;;GAMG;AACH,6CAJc,MAAM,EAAA,eASnB;mBA1GkB,UAAU;AAI7B;IACE,0BAGC;IAED,cAoDC;IAED,cAEC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAQC;IAMD,gCASC;CACF"}