@atscript/typescript 0.0.16 → 0.0.17

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/cli.cjs CHANGED
@@ -204,7 +204,7 @@ function wrapProp(name) {
204
204
  return name;
205
205
  }
206
206
  function escapeQuotes(str) {
207
- return str.replace(/"/g, "\\\"");
207
+ return str.replace(/"/g, "\\\"").replace(/\\/g, "\\\\");
208
208
  }
209
209
 
210
210
  //#endregion
@@ -219,7 +219,7 @@ var TypeRenderer = class extends BaseRenderer {
219
219
  this.writeln(" * Do not edit this file!");
220
220
  this.writeln(" */");
221
221
  this.writeln();
222
- this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator } from \"@atscript/typescript\"");
222
+ this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript\"");
223
223
  }
224
224
  post() {
225
225
  this.writeln("// prettier-ignore-end");
@@ -278,10 +278,10 @@ var TypeRenderer = class extends BaseRenderer {
278
278
  this.writeln();
279
279
  }
280
280
  if (asClass) {
281
- this.writeln("static __is_anscript_annotated_type: true");
281
+ this.writeln("static __is_atscript_annotated_type: true");
282
282
  this.writeln(`static type: TAtscriptTypeObject<keyof ${asClass}>`);
283
283
  this.writeln(`static metadata: TMetadataMap<AtscriptMetadata>`);
284
- this.writeln(`static validator: () => Validator<${asClass}>`);
284
+ this.writeln(`static validator: <TT extends TAtscriptAnnotatedTypeConstructor = ${asClass}>(opts?: TValidatorOptions) => Validator<TT>`);
285
285
  }
286
286
  this.pop();
287
287
  }
@@ -320,10 +320,10 @@ else if ((0, __atscript_core.isGroup)(realDef)) typeDef = "TAtscriptTypeComplex"
320
320
  else if ((0, __atscript_core.isArray)(realDef)) typeDef = "TAtscriptTypeArray";
321
321
  else if ((0, __atscript_core.isPrimitive)(realDef)) typeDef = "TAtscriptTypeFinal";
322
322
  }
323
- this.writeln(`const __is_anscript_annotated_type: true`);
323
+ this.writeln(`const __is_atscript_annotated_type: true`);
324
324
  this.writeln(`const type: ${typeDef}`);
325
325
  this.writeln(`const metadata: TMetadataMap<AtscriptMetadata>`);
326
- this.writeln(`const validator: () => Validator<${node.id}>`);
326
+ this.writeln(`static validator: <TT extends TAtscriptAnnotatedTypeConstructor = ${node.id}>(opts?: TValidatorOptions) => Validator<TT>`);
327
327
  this.popln();
328
328
  }
329
329
  renderJsDoc(node) {
@@ -382,7 +382,7 @@ var JsRenderer = class extends BaseRenderer {
382
382
  this.write(exported ? "export " : "");
383
383
  this.write(`class ${node.id} `);
384
384
  this.blockln("{}");
385
- this.writeln("static __is_anscript_annotated_type = true");
385
+ this.writeln("static __is_atscript_annotated_type = true");
386
386
  this.writeln("static type = {}");
387
387
  this.writeln("static metadata = new Map()");
388
388
  this.popln();
@@ -395,7 +395,7 @@ var JsRenderer = class extends BaseRenderer {
395
395
  this.write(exported ? "export " : "");
396
396
  this.write(`class ${node.id} `);
397
397
  this.blockln("{}");
398
- this.writeln("static __is_anscript_annotated_type = true");
398
+ this.writeln("static __is_atscript_annotated_type = true");
399
399
  this.writeln("static type = {}");
400
400
  this.writeln("static metadata = new Map()");
401
401
  this.popln();
package/dist/index.cjs CHANGED
@@ -202,7 +202,7 @@ function wrapProp(name) {
202
202
  return name;
203
203
  }
204
204
  function escapeQuotes(str) {
205
- return str.replace(/"/g, "\\\"");
205
+ return str.replace(/"/g, "\\\"").replace(/\\/g, "\\\\");
206
206
  }
207
207
 
208
208
  //#endregion
@@ -217,7 +217,7 @@ var TypeRenderer = class extends BaseRenderer {
217
217
  this.writeln(" * Do not edit this file!");
218
218
  this.writeln(" */");
219
219
  this.writeln();
220
- this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator } from \"@atscript/typescript\"");
220
+ this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript\"");
221
221
  }
222
222
  post() {
223
223
  this.writeln("// prettier-ignore-end");
@@ -276,10 +276,10 @@ var TypeRenderer = class extends BaseRenderer {
276
276
  this.writeln();
277
277
  }
278
278
  if (asClass) {
279
- this.writeln("static __is_anscript_annotated_type: true");
279
+ this.writeln("static __is_atscript_annotated_type: true");
280
280
  this.writeln(`static type: TAtscriptTypeObject<keyof ${asClass}>`);
281
281
  this.writeln(`static metadata: TMetadataMap<AtscriptMetadata>`);
282
- this.writeln(`static validator: () => Validator<${asClass}>`);
282
+ this.writeln(`static validator: <TT extends TAtscriptAnnotatedTypeConstructor = ${asClass}>(opts?: TValidatorOptions) => Validator<TT>`);
283
283
  }
284
284
  this.pop();
285
285
  }
@@ -318,10 +318,10 @@ else if ((0, __atscript_core.isGroup)(realDef)) typeDef = "TAtscriptTypeComplex"
318
318
  else if ((0, __atscript_core.isArray)(realDef)) typeDef = "TAtscriptTypeArray";
319
319
  else if ((0, __atscript_core.isPrimitive)(realDef)) typeDef = "TAtscriptTypeFinal";
320
320
  }
321
- this.writeln(`const __is_anscript_annotated_type: true`);
321
+ this.writeln(`const __is_atscript_annotated_type: true`);
322
322
  this.writeln(`const type: ${typeDef}`);
323
323
  this.writeln(`const metadata: TMetadataMap<AtscriptMetadata>`);
324
- this.writeln(`const validator: () => Validator<${node.id}>`);
324
+ this.writeln(`static validator: <TT extends TAtscriptAnnotatedTypeConstructor = ${node.id}>(opts?: TValidatorOptions) => Validator<TT>`);
325
325
  this.popln();
326
326
  }
327
327
  renderJsDoc(node) {
@@ -380,7 +380,7 @@ var JsRenderer = class extends BaseRenderer {
380
380
  this.write(exported ? "export " : "");
381
381
  this.write(`class ${node.id} `);
382
382
  this.blockln("{}");
383
- this.writeln("static __is_anscript_annotated_type = true");
383
+ this.writeln("static __is_atscript_annotated_type = true");
384
384
  this.writeln("static type = {}");
385
385
  this.writeln("static metadata = new Map()");
386
386
  this.popln();
@@ -393,7 +393,7 @@ var JsRenderer = class extends BaseRenderer {
393
393
  this.write(exported ? "export " : "");
394
394
  this.write(`class ${node.id} `);
395
395
  this.blockln("{}");
396
- this.writeln("static __is_anscript_annotated_type = true");
396
+ this.writeln("static __is_atscript_annotated_type = true");
397
397
  this.writeln("static type = {}");
398
398
  this.writeln("static metadata = new Map()");
399
399
  this.popln();
@@ -763,10 +763,24 @@ var Validator = class {
763
763
  let passed = true;
764
764
  const valueKeys = new Set(Object.keys(value));
765
765
  const typeKeys = new Set();
766
+ const skipList = new Set();
767
+ if (this.opts.skipList) {
768
+ const path$2 = this.stackPath.length > 1 ? this.stackPath.slice(1).join(".") + "." : "";
769
+ this.opts.skipList.forEach((item) => {
770
+ if (item.startsWith(path$2)) {
771
+ const key = item.slice(path$2.length);
772
+ skipList.add(key);
773
+ valueKeys.delete(key);
774
+ }
775
+ });
776
+ }
777
+ let partialFunctionMatched = false;
778
+ if (typeof this.opts.partial === "function") partialFunctionMatched = this.opts.partial(def, this.stackPath.join(".").slice(1));
766
779
  for (const [key, item] of def.type.props.entries()) {
780
+ if (skipList.has(key)) continue;
767
781
  typeKeys.add(key);
768
782
  if (value[key] === undefined) {
769
- if (this.opts.partial === "deep" || this.opts.partial === true && this.stackPath.length <= 1) continue;
783
+ if (partialFunctionMatched || this.opts.partial === "deep" || this.opts.partial === true && this.stackPath.length <= 1) continue;
770
784
  }
771
785
  this.push(key);
772
786
  if (this._validate(item, value[key])) this.pop(false);
@@ -908,7 +922,7 @@ var ValidatorError = class extends Error {
908
922
  //#endregion
909
923
  //#region packages/typescript/src/annotated-type.ts
910
924
  function isAnnotatedType(type) {
911
- return type && type.__is_anscript_annotated_type;
925
+ return type && type.__is_atscript_annotated_type;
912
926
  }
913
927
  function defineAnnotatedType(_kind, base) {
914
928
  const kind = _kind || "";
@@ -923,7 +937,7 @@ function defineAnnotatedType(_kind, base) {
923
937
  type.tags = new Set();
924
938
  const metadata = base?.metadata || new Map();
925
939
  if (base) Object.assign(base, {
926
- __is_anscript_annotated_type: true,
940
+ __is_atscript_annotated_type: true,
927
941
  metadata,
928
942
  type,
929
943
  validator(opts) {
@@ -931,7 +945,7 @@ function defineAnnotatedType(_kind, base) {
931
945
  }
932
946
  });
933
947
  else base = {
934
- __is_anscript_annotated_type: true,
948
+ __is_atscript_annotated_type: true,
935
949
  metadata,
936
950
  type,
937
951
  validator(opts) {
@@ -984,7 +998,7 @@ else throw new Error(`Can't find prop ${typeName}${keys}`);
984
998
  if (!newBase && keys) throw new Error(`Can't find prop ${typeName}${keys}`);
985
999
  else if (!newBase) throw new Error(`"${typeName}" is not annotated type`);
986
1000
  this.$type = {
987
- __is_anscript_annotated_type: true,
1001
+ __is_atscript_annotated_type: true,
988
1002
  type: newBase.type,
989
1003
  metadata,
990
1004
  validator(opts) {
package/dist/index.d.ts CHANGED
@@ -8,9 +8,10 @@ interface TError {
8
8
  details?: TError[];
9
9
  }
10
10
  interface TValidatorOptions {
11
- partial: boolean | 'deep';
11
+ partial: boolean | 'deep' | ((type: TAtscriptAnnotatedType<TAtscriptTypeObject>, path: string) => boolean);
12
12
  unknwonProps: 'strip' | 'ignore' | 'error';
13
13
  errorLimit: number;
14
+ skipList?: Set<string>;
14
15
  }
15
16
  declare class Validator<T extends TAtscriptAnnotatedTypeConstructor> {
16
17
  protected readonly def: T;
@@ -25,7 +26,7 @@ declare class Validator<T extends TAtscriptAnnotatedTypeConstructor> {
25
26
  protected clear(): void;
26
27
  protected error(message: string, path?: string, details?: TError[]): void;
27
28
  protected throw(): void;
28
- validate<TT = InstanceType<T>>(value: any, safe?: boolean): value is TT;
29
+ validate<TT = T>(value: any, safe?: boolean): value is TT;
29
30
  protected _validate(def: TAtscriptAnnotatedType, value: any): boolean;
30
31
  protected validateUnion(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
31
32
  protected validateIntersection(def: TAtscriptAnnotatedType<TAtscriptTypeComplex>, value: any): boolean;
@@ -65,9 +66,9 @@ interface TAtscriptTypeFinal {
65
66
  }
66
67
  type TAtscriptTypeDef = TAtscriptTypeComplex | TAtscriptTypeFinal | TAtscriptTypeArray | TAtscriptTypeObject<string>;
67
68
  interface TAtscriptAnnotatedType<T = TAtscriptTypeDef> {
68
- __is_anscript_annotated_type: true;
69
+ __is_atscript_annotated_type: true;
69
70
  type: T;
70
- validator: (opts?: TValidatorOptions) => Validator<any>;
71
+ validator: <TT extends TAtscriptAnnotatedTypeConstructor>(opts?: Partial<TValidatorOptions>) => Validator<TT>;
71
72
  metadata: TMetadataMap<AtscriptMetadata>;
72
73
  optional?: boolean;
73
74
  }
package/dist/index.mjs CHANGED
@@ -177,7 +177,7 @@ function wrapProp(name) {
177
177
  return name;
178
178
  }
179
179
  function escapeQuotes(str) {
180
- return str.replace(/"/g, "\\\"");
180
+ return str.replace(/"/g, "\\\"").replace(/\\/g, "\\\\");
181
181
  }
182
182
 
183
183
  //#endregion
@@ -192,7 +192,7 @@ var TypeRenderer = class extends BaseRenderer {
192
192
  this.writeln(" * Do not edit this file!");
193
193
  this.writeln(" */");
194
194
  this.writeln();
195
- this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator } from \"@atscript/typescript\"");
195
+ this.writeln("import type { TAtscriptTypeObject, TAtscriptTypeComplex, TAtscriptTypeFinal, TAtscriptTypeArray, TMetadataMap, Validator, TAtscriptAnnotatedTypeConstructor, TValidatorOptions } from \"@atscript/typescript\"");
196
196
  }
197
197
  post() {
198
198
  this.writeln("// prettier-ignore-end");
@@ -251,10 +251,10 @@ var TypeRenderer = class extends BaseRenderer {
251
251
  this.writeln();
252
252
  }
253
253
  if (asClass) {
254
- this.writeln("static __is_anscript_annotated_type: true");
254
+ this.writeln("static __is_atscript_annotated_type: true");
255
255
  this.writeln(`static type: TAtscriptTypeObject<keyof ${asClass}>`);
256
256
  this.writeln(`static metadata: TMetadataMap<AtscriptMetadata>`);
257
- this.writeln(`static validator: () => Validator<${asClass}>`);
257
+ this.writeln(`static validator: <TT extends TAtscriptAnnotatedTypeConstructor = ${asClass}>(opts?: TValidatorOptions) => Validator<TT>`);
258
258
  }
259
259
  this.pop();
260
260
  }
@@ -293,10 +293,10 @@ else if (isGroup(realDef)) typeDef = "TAtscriptTypeComplex";
293
293
  else if (isArray(realDef)) typeDef = "TAtscriptTypeArray";
294
294
  else if (isPrimitive(realDef)) typeDef = "TAtscriptTypeFinal";
295
295
  }
296
- this.writeln(`const __is_anscript_annotated_type: true`);
296
+ this.writeln(`const __is_atscript_annotated_type: true`);
297
297
  this.writeln(`const type: ${typeDef}`);
298
298
  this.writeln(`const metadata: TMetadataMap<AtscriptMetadata>`);
299
- this.writeln(`const validator: () => Validator<${node.id}>`);
299
+ this.writeln(`static validator: <TT extends TAtscriptAnnotatedTypeConstructor = ${node.id}>(opts?: TValidatorOptions) => Validator<TT>`);
300
300
  this.popln();
301
301
  }
302
302
  renderJsDoc(node) {
@@ -355,7 +355,7 @@ var JsRenderer = class extends BaseRenderer {
355
355
  this.write(exported ? "export " : "");
356
356
  this.write(`class ${node.id} `);
357
357
  this.blockln("{}");
358
- this.writeln("static __is_anscript_annotated_type = true");
358
+ this.writeln("static __is_atscript_annotated_type = true");
359
359
  this.writeln("static type = {}");
360
360
  this.writeln("static metadata = new Map()");
361
361
  this.popln();
@@ -368,7 +368,7 @@ var JsRenderer = class extends BaseRenderer {
368
368
  this.write(exported ? "export " : "");
369
369
  this.write(`class ${node.id} `);
370
370
  this.blockln("{}");
371
- this.writeln("static __is_anscript_annotated_type = true");
371
+ this.writeln("static __is_atscript_annotated_type = true");
372
372
  this.writeln("static type = {}");
373
373
  this.writeln("static metadata = new Map()");
374
374
  this.popln();
@@ -738,10 +738,24 @@ var Validator = class {
738
738
  let passed = true;
739
739
  const valueKeys = new Set(Object.keys(value));
740
740
  const typeKeys = new Set();
741
+ const skipList = new Set();
742
+ if (this.opts.skipList) {
743
+ const path$1 = this.stackPath.length > 1 ? this.stackPath.slice(1).join(".") + "." : "";
744
+ this.opts.skipList.forEach((item) => {
745
+ if (item.startsWith(path$1)) {
746
+ const key = item.slice(path$1.length);
747
+ skipList.add(key);
748
+ valueKeys.delete(key);
749
+ }
750
+ });
751
+ }
752
+ let partialFunctionMatched = false;
753
+ if (typeof this.opts.partial === "function") partialFunctionMatched = this.opts.partial(def, this.stackPath.join(".").slice(1));
741
754
  for (const [key, item] of def.type.props.entries()) {
755
+ if (skipList.has(key)) continue;
742
756
  typeKeys.add(key);
743
757
  if (value[key] === undefined) {
744
- if (this.opts.partial === "deep" || this.opts.partial === true && this.stackPath.length <= 1) continue;
758
+ if (partialFunctionMatched || this.opts.partial === "deep" || this.opts.partial === true && this.stackPath.length <= 1) continue;
745
759
  }
746
760
  this.push(key);
747
761
  if (this._validate(item, value[key])) this.pop(false);
@@ -883,7 +897,7 @@ var ValidatorError = class extends Error {
883
897
  //#endregion
884
898
  //#region packages/typescript/src/annotated-type.ts
885
899
  function isAnnotatedType(type) {
886
- return type && type.__is_anscript_annotated_type;
900
+ return type && type.__is_atscript_annotated_type;
887
901
  }
888
902
  function defineAnnotatedType(_kind, base) {
889
903
  const kind = _kind || "";
@@ -898,7 +912,7 @@ function defineAnnotatedType(_kind, base) {
898
912
  type.tags = new Set();
899
913
  const metadata = base?.metadata || new Map();
900
914
  if (base) Object.assign(base, {
901
- __is_anscript_annotated_type: true,
915
+ __is_atscript_annotated_type: true,
902
916
  metadata,
903
917
  type,
904
918
  validator(opts) {
@@ -906,7 +920,7 @@ function defineAnnotatedType(_kind, base) {
906
920
  }
907
921
  });
908
922
  else base = {
909
- __is_anscript_annotated_type: true,
923
+ __is_atscript_annotated_type: true,
910
924
  metadata,
911
925
  type,
912
926
  validator(opts) {
@@ -959,7 +973,7 @@ else throw new Error(`Can't find prop ${typeName}${keys}`);
959
973
  if (!newBase && keys) throw new Error(`Can't find prop ${typeName}${keys}`);
960
974
  else if (!newBase) throw new Error(`"${typeName}" is not annotated type`);
961
975
  this.$type = {
962
- __is_anscript_annotated_type: true,
976
+ __is_atscript_annotated_type: true,
963
977
  type: newBase.type,
964
978
  metadata,
965
979
  validator(opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/typescript",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "Atscript: typescript-gen support.",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -49,7 +49,7 @@
49
49
  "dependencies": {
50
50
  "@moostjs/event-cli": "^0.5.26",
51
51
  "moost": "^0.5.26",
52
- "@atscript/core": "^0.0.16"
52
+ "@atscript/core": "^0.0.17"
53
53
  },
54
54
  "devDependencies": {
55
55
  "vitest": "^3.0.0"