model-data-validator.spec.js 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829
  1. import {expect} from 'chai';
  2. import {Schema} from '../../schema.js';
  3. import {format} from '@e22m4u/js-format';
  4. import {DataType} from './properties/index.js';
  5. import {EmptyValuesDefiner} from './properties/index.js';
  6. import {ModelDataValidator} from './model-data-validator.js';
  7. import {DefinitionRegistry} from '../definition-registry.js';
  8. import {PropertyValidatorRegistry} from './properties/index.js';
  9. describe('ModelDataValidator', function () {
  10. describe('validate', function () {
  11. it('does not throw an error if a model does not have a property of a given data', function () {
  12. const schema = new Schema();
  13. schema.defineModel({name: 'model'});
  14. schema.getService(ModelDataValidator).validate('model', {foo: 'bar'});
  15. });
  16. it('throws an error if a given data is not a pure object', function () {
  17. const throwable = modelData => () => {
  18. const schema = new Schema();
  19. schema.defineModel({
  20. name: 'model',
  21. datasource: 'datasource',
  22. });
  23. schema.getService(ModelDataValidator).validate('model', modelData);
  24. };
  25. const error = v =>
  26. format(
  27. 'The data of the model "model" should be an Object, but %s given.',
  28. v,
  29. );
  30. expect(throwable('str')).to.throw(error('"str"'));
  31. expect(throwable(10)).to.throw(error('10'));
  32. expect(throwable(true)).to.throw(error('true'));
  33. expect(throwable(false)).to.throw(error('false'));
  34. expect(throwable([])).to.throw(error('Array'));
  35. expect(throwable(null)).to.throw(error('null'));
  36. expect(throwable(undefined)).to.throw(error('undefined'));
  37. });
  38. it('uses a base model hierarchy to validate a given data', function () {
  39. const schema = new Schema();
  40. schema.defineModel({
  41. name: 'modelA',
  42. properties: {
  43. foo: DataType.STRING,
  44. },
  45. });
  46. schema.defineModel({
  47. name: 'modelB',
  48. base: 'modelA',
  49. });
  50. const throwable = () =>
  51. schema.getService(ModelDataValidator).validate('modelB', {foo: 10});
  52. expect(throwable).to.throw(
  53. 'The property "foo" of the model "modelB" must ' +
  54. 'have a String, but Number given.',
  55. );
  56. });
  57. it('throws an error if a given data does not have a required property', function () {
  58. const schema = new Schema();
  59. schema.defineModel({
  60. name: 'model',
  61. properties: {
  62. foo: {
  63. type: DataType.STRING,
  64. required: true,
  65. },
  66. },
  67. });
  68. const throwable = () =>
  69. schema.getService(ModelDataValidator).validate('model', {});
  70. expect(throwable).to.throw(
  71. 'The property "foo" of the model "model" ' +
  72. 'is required, but undefined given.',
  73. );
  74. });
  75. it('throws an error if a required property is undefined', function () {
  76. const schema = new Schema();
  77. schema.defineModel({
  78. name: 'model',
  79. properties: {
  80. foo: {
  81. type: DataType.STRING,
  82. required: true,
  83. },
  84. },
  85. });
  86. const throwable = () =>
  87. schema
  88. .getService(ModelDataValidator)
  89. .validate('model', {foo: undefined});
  90. expect(throwable).to.throw(
  91. 'The property "foo" of the model "model" is required, but undefined given.',
  92. );
  93. });
  94. it('throws an error if a required property is null', function () {
  95. const schema = new Schema();
  96. schema.defineModel({
  97. name: 'model',
  98. properties: {
  99. foo: {
  100. type: DataType.STRING,
  101. required: true,
  102. },
  103. },
  104. });
  105. const throwable = () =>
  106. schema.getService(ModelDataValidator).validate('model', {foo: null});
  107. expect(throwable).to.throw(
  108. 'The property "foo" of the model "model" is required, but null given.',
  109. );
  110. });
  111. it('throws an error if a required property has an empty value', function () {
  112. const schema = new Schema();
  113. schema.defineModel({
  114. name: 'model',
  115. properties: {
  116. foo: {
  117. type: DataType.STRING,
  118. required: true,
  119. },
  120. },
  121. });
  122. schema
  123. .getService(EmptyValuesDefiner)
  124. .setEmptyValuesOf(DataType.STRING, ['empty']);
  125. const throwable = () =>
  126. schema.getService(ModelDataValidator).validate('model', {foo: 'empty'});
  127. expect(throwable).to.throw(
  128. 'The property "foo" of the model "model" ' +
  129. 'is required, but "empty" given.',
  130. );
  131. });
  132. describe('an option "isPartial" is true', function () {
  133. it('does not throw an error if a given data does not have a required property', function () {
  134. const schema = new Schema();
  135. schema.defineModel({
  136. name: 'model',
  137. properties: {
  138. foo: {
  139. type: DataType.STRING,
  140. required: true,
  141. },
  142. },
  143. });
  144. schema.getService(ModelDataValidator).validate('model', {}, true);
  145. });
  146. it('throws an error if a required property is undefined', function () {
  147. const schema = new Schema();
  148. schema.defineModel({
  149. name: 'model',
  150. properties: {
  151. foo: {
  152. type: DataType.STRING,
  153. required: true,
  154. },
  155. },
  156. });
  157. const throwable = () =>
  158. schema
  159. .getService(ModelDataValidator)
  160. .validate('model', {foo: undefined}, true);
  161. expect(throwable).to.throw(
  162. 'The property "foo" of the model "model" ' +
  163. 'is required, but undefined given.',
  164. );
  165. });
  166. it('throws an error if a required property is null', function () {
  167. const schema = new Schema();
  168. schema.defineModel({
  169. name: 'model',
  170. properties: {
  171. foo: {
  172. type: DataType.STRING,
  173. required: true,
  174. },
  175. },
  176. });
  177. const throwable = () =>
  178. schema
  179. .getService(ModelDataValidator)
  180. .validate('model', {foo: null}, true);
  181. expect(throwable).to.throw(
  182. 'The property "foo" of the model "model" is required, but null given.',
  183. );
  184. });
  185. it('throws an error if a required property has an empty value', function () {
  186. const schema = new Schema();
  187. schema.defineModel({
  188. name: 'model',
  189. properties: {
  190. foo: {
  191. type: DataType.STRING,
  192. required: true,
  193. },
  194. },
  195. });
  196. schema
  197. .getService(EmptyValuesDefiner)
  198. .setEmptyValuesOf(DataType.STRING, [5]);
  199. const throwable = () =>
  200. schema
  201. .getService(ModelDataValidator)
  202. .validate('model', {foo: 5}, true);
  203. expect(throwable).to.throw(
  204. 'The property "foo" of the model "model" is required, but 5 given.',
  205. );
  206. });
  207. });
  208. describe('validate by property type', function () {
  209. it('skips validation for an empty value', function () {
  210. const S = new Schema();
  211. S.defineModel({
  212. name: 'model',
  213. datasource: 'datasource',
  214. properties: {
  215. foo: {
  216. type: DataType.STRING,
  217. },
  218. },
  219. });
  220. S.getService(EmptyValuesDefiner).setEmptyValuesOf(DataType.STRING, [5]);
  221. S.getService(ModelDataValidator).validate('model', {foo: 5});
  222. });
  223. describe('DataType.ANY', function () {
  224. describe('ShortPropertyDefinition', function () {
  225. it('does not throw an error if an undefined given', function () {
  226. const S = new Schema();
  227. S.defineModel({
  228. name: 'model',
  229. datasource: 'datasource',
  230. properties: {
  231. foo: DataType.ANY,
  232. },
  233. });
  234. S.getService(ModelDataValidator).validate('model', {
  235. foo: undefined,
  236. });
  237. });
  238. it('does not throw an error if a null given', function () {
  239. const S = new Schema();
  240. S.defineModel({
  241. name: 'model',
  242. datasource: 'datasource',
  243. properties: {
  244. foo: DataType.ANY,
  245. },
  246. });
  247. S.getService(ModelDataValidator).validate('model', {
  248. foo: null,
  249. });
  250. });
  251. it('does not throw an error if a string given', function () {
  252. const S = new Schema();
  253. S.defineModel({
  254. name: 'model',
  255. datasource: 'datasource',
  256. properties: {
  257. foo: DataType.ANY,
  258. },
  259. });
  260. S.getService(ModelDataValidator).validate('model', {
  261. foo: 'bar',
  262. });
  263. });
  264. it('does not throw an error if a number given', function () {
  265. const S = new Schema();
  266. S.defineModel({
  267. name: 'model',
  268. datasource: 'datasource',
  269. properties: {
  270. foo: DataType.ANY,
  271. },
  272. });
  273. S.getService(ModelDataValidator).validate('model', {
  274. foo: 10,
  275. });
  276. });
  277. it('does not throw an error if true given', function () {
  278. const S = new Schema();
  279. S.defineModel({
  280. name: 'model',
  281. datasource: 'datasource',
  282. properties: {
  283. foo: DataType.ANY,
  284. },
  285. });
  286. S.getService(ModelDataValidator).validate('model', {
  287. foo: true,
  288. });
  289. });
  290. it('does not throw an error if false given', function () {
  291. const S = new Schema();
  292. S.defineModel({
  293. name: 'model',
  294. datasource: 'datasource',
  295. properties: {
  296. foo: DataType.ANY,
  297. },
  298. });
  299. S.getService(ModelDataValidator).validate('model', {
  300. foo: false,
  301. });
  302. });
  303. it('does not throw an error if an array given', function () {
  304. const S = new Schema();
  305. S.defineModel({
  306. name: 'model',
  307. datasource: 'datasource',
  308. properties: {
  309. foo: DataType.ANY,
  310. },
  311. });
  312. S.getService(ModelDataValidator).validate('model', {
  313. foo: [],
  314. });
  315. });
  316. it('does not throw an error if an object given', function () {
  317. const S = new Schema();
  318. S.defineModel({
  319. name: 'model',
  320. datasource: 'datasource',
  321. properties: {
  322. foo: DataType.ANY,
  323. },
  324. });
  325. S.getService(ModelDataValidator).validate('model', {
  326. foo: {},
  327. });
  328. });
  329. });
  330. describe('FullPropertyDefinition', function () {
  331. it('does not throw an error if an undefined given', function () {
  332. const S = new Schema();
  333. S.defineModel({
  334. name: 'model',
  335. datasource: 'datasource',
  336. properties: {
  337. foo: {
  338. type: DataType.ANY,
  339. },
  340. },
  341. });
  342. S.getService(ModelDataValidator).validate('model', {
  343. foo: undefined,
  344. });
  345. });
  346. it('does not throw an error if a null given', function () {
  347. const S = new Schema();
  348. S.defineModel({
  349. name: 'model',
  350. datasource: 'datasource',
  351. properties: {
  352. foo: {
  353. type: DataType.ANY,
  354. },
  355. },
  356. });
  357. S.getService(ModelDataValidator).validate('model', {
  358. foo: null,
  359. });
  360. });
  361. it('does not throw an error if a string given', function () {
  362. const S = new Schema();
  363. S.defineModel({
  364. name: 'model',
  365. datasource: 'datasource',
  366. properties: {
  367. foo: {
  368. type: DataType.ANY,
  369. },
  370. },
  371. });
  372. S.getService(ModelDataValidator).validate('model', {
  373. foo: 'bar',
  374. });
  375. });
  376. it('does not throw an error if a number given', function () {
  377. const S = new Schema();
  378. S.defineModel({
  379. name: 'model',
  380. datasource: 'datasource',
  381. properties: {
  382. foo: {
  383. type: DataType.ANY,
  384. },
  385. },
  386. });
  387. S.getService(ModelDataValidator).validate('model', {
  388. foo: 10,
  389. });
  390. });
  391. it('does not throw an error if true given', function () {
  392. const S = new Schema();
  393. S.defineModel({
  394. name: 'model',
  395. datasource: 'datasource',
  396. properties: {
  397. foo: {
  398. type: DataType.ANY,
  399. },
  400. },
  401. });
  402. S.getService(ModelDataValidator).validate('model', {
  403. foo: true,
  404. });
  405. });
  406. it('does not throw an error if false given', function () {
  407. const S = new Schema();
  408. S.defineModel({
  409. name: 'model',
  410. datasource: 'datasource',
  411. properties: {
  412. foo: {
  413. type: DataType.ANY,
  414. },
  415. },
  416. });
  417. S.getService(ModelDataValidator).validate('model', {
  418. foo: false,
  419. });
  420. });
  421. it('does not throw an error if an array given', function () {
  422. const S = new Schema();
  423. S.defineModel({
  424. name: 'model',
  425. datasource: 'datasource',
  426. properties: {
  427. foo: {
  428. type: DataType.ANY,
  429. },
  430. },
  431. });
  432. S.getService(ModelDataValidator).validate('model', {
  433. foo: [],
  434. });
  435. });
  436. it('does not throw an error if an object given', function () {
  437. const S = new Schema();
  438. S.defineModel({
  439. name: 'model',
  440. datasource: 'datasource',
  441. properties: {
  442. foo: {
  443. type: DataType.ANY,
  444. },
  445. },
  446. });
  447. S.getService(ModelDataValidator).validate('model', {
  448. foo: {},
  449. });
  450. });
  451. });
  452. });
  453. describe('DataType.STRING', function () {
  454. describe('ShortPropertyDefinition', function () {
  455. it('does not throw an error if an undefined given', function () {
  456. const S = new Schema();
  457. S.defineModel({
  458. name: 'model',
  459. datasource: 'datasource',
  460. properties: {
  461. foo: DataType.STRING,
  462. },
  463. });
  464. S.getService(ModelDataValidator).validate('model', {
  465. foo: undefined,
  466. });
  467. });
  468. it('does not throw an error if a null given', function () {
  469. const S = new Schema();
  470. S.defineModel({
  471. name: 'model',
  472. datasource: 'datasource',
  473. properties: {
  474. foo: DataType.STRING,
  475. },
  476. });
  477. S.getService(ModelDataValidator).validate('model', {
  478. foo: null,
  479. });
  480. });
  481. it('does not throw an error if a string given', function () {
  482. const S = new Schema();
  483. S.defineModel({
  484. name: 'model',
  485. datasource: 'datasource',
  486. properties: {
  487. foo: DataType.STRING,
  488. },
  489. });
  490. S.getService(ModelDataValidator).validate('model', {
  491. foo: 'bar',
  492. });
  493. });
  494. it('throws an error if a number given', function () {
  495. const S = new Schema();
  496. S.defineModel({
  497. name: 'model',
  498. datasource: 'datasource',
  499. properties: {
  500. foo: DataType.STRING,
  501. },
  502. });
  503. const throwable = () =>
  504. S.getService(ModelDataValidator).validate('model', {
  505. foo: 10,
  506. });
  507. expect(throwable).to.throw(
  508. 'The property "foo" of the model "model" must have ' +
  509. 'a String, but Number given.',
  510. );
  511. });
  512. it('throws an error if true given', function () {
  513. const S = new Schema();
  514. S.defineModel({
  515. name: 'model',
  516. datasource: 'datasource',
  517. properties: {
  518. foo: DataType.STRING,
  519. },
  520. });
  521. const throwable = () =>
  522. S.getService(ModelDataValidator).validate('model', {
  523. foo: true,
  524. });
  525. expect(throwable).to.throw(
  526. 'The property "foo" of the model "model" must have ' +
  527. 'a String, but Boolean given.',
  528. );
  529. });
  530. it('throws an error if false given', function () {
  531. const S = new Schema();
  532. S.defineModel({
  533. name: 'model',
  534. datasource: 'datasource',
  535. properties: {
  536. foo: DataType.STRING,
  537. },
  538. });
  539. const throwable = () =>
  540. S.getService(ModelDataValidator).validate('model', {
  541. foo: false,
  542. });
  543. expect(throwable).to.throw(
  544. 'The property "foo" of the model "model" must have ' +
  545. 'a String, but Boolean given.',
  546. );
  547. });
  548. it('throws an error if an array given', function () {
  549. const S = new Schema();
  550. S.defineModel({
  551. name: 'model',
  552. datasource: 'datasource',
  553. properties: {
  554. foo: DataType.STRING,
  555. },
  556. });
  557. const throwable = () =>
  558. S.getService(ModelDataValidator).validate('model', {
  559. foo: [],
  560. });
  561. expect(throwable).to.throw(
  562. 'The property "foo" of the model "model" must have ' +
  563. 'a String, but Array given.',
  564. );
  565. });
  566. it('throws an error if an object given', function () {
  567. const S = new Schema();
  568. S.defineModel({
  569. name: 'model',
  570. datasource: 'datasource',
  571. properties: {
  572. foo: DataType.STRING,
  573. },
  574. });
  575. const throwable = () =>
  576. S.getService(ModelDataValidator).validate('model', {
  577. foo: {},
  578. });
  579. expect(throwable).to.throw(
  580. 'The property "foo" of the model "model" must have ' +
  581. 'a String, but Object given.',
  582. );
  583. });
  584. });
  585. describe('FullPropertyDefinition', function () {
  586. it('does not throw an error if an undefined given', function () {
  587. const S = new Schema();
  588. S.defineModel({
  589. name: 'model',
  590. datasource: 'datasource',
  591. properties: {
  592. foo: {
  593. type: DataType.STRING,
  594. },
  595. },
  596. });
  597. S.getService(ModelDataValidator).validate('model', {
  598. foo: undefined,
  599. });
  600. });
  601. it('does not throw an error if a null given', function () {
  602. const S = new Schema();
  603. S.defineModel({
  604. name: 'model',
  605. datasource: 'datasource',
  606. properties: {
  607. foo: {
  608. type: DataType.STRING,
  609. },
  610. },
  611. });
  612. S.getService(ModelDataValidator).validate('model', {
  613. foo: null,
  614. });
  615. });
  616. it('does not throw an error if a string given', function () {
  617. const S = new Schema();
  618. S.defineModel({
  619. name: 'model',
  620. datasource: 'datasource',
  621. properties: {
  622. foo: {
  623. type: DataType.STRING,
  624. },
  625. },
  626. });
  627. S.getService(ModelDataValidator).validate('model', {
  628. foo: 'bar',
  629. });
  630. });
  631. it('throws an error if a number given', function () {
  632. const S = new Schema();
  633. S.defineModel({
  634. name: 'model',
  635. datasource: 'datasource',
  636. properties: {
  637. foo: {
  638. type: DataType.STRING,
  639. },
  640. },
  641. });
  642. const throwable = () =>
  643. S.getService(ModelDataValidator).validate('model', {
  644. foo: 10,
  645. });
  646. expect(throwable).to.throw(
  647. 'The property "foo" of the model "model" must have ' +
  648. 'a String, but Number given.',
  649. );
  650. });
  651. it('throws an error if true given', function () {
  652. const S = new Schema();
  653. S.defineModel({
  654. name: 'model',
  655. datasource: 'datasource',
  656. properties: {
  657. foo: {
  658. type: DataType.STRING,
  659. },
  660. },
  661. });
  662. const throwable = () =>
  663. S.getService(ModelDataValidator).validate('model', {
  664. foo: true,
  665. });
  666. expect(throwable).to.throw(
  667. 'The property "foo" of the model "model" must have ' +
  668. 'a String, but Boolean given.',
  669. );
  670. });
  671. it('throws an error if false given', function () {
  672. const S = new Schema();
  673. S.defineModel({
  674. name: 'model',
  675. datasource: 'datasource',
  676. properties: {
  677. foo: {
  678. type: DataType.STRING,
  679. },
  680. },
  681. });
  682. const throwable = () =>
  683. S.getService(ModelDataValidator).validate('model', {
  684. foo: false,
  685. });
  686. expect(throwable).to.throw(
  687. 'The property "foo" of the model "model" must have ' +
  688. 'a String, but Boolean given.',
  689. );
  690. });
  691. it('throws an error if an array given', function () {
  692. const S = new Schema();
  693. S.defineModel({
  694. name: 'model',
  695. datasource: 'datasource',
  696. properties: {
  697. foo: {
  698. type: DataType.STRING,
  699. },
  700. },
  701. });
  702. const throwable = () =>
  703. S.getService(ModelDataValidator).validate('model', {
  704. foo: [],
  705. });
  706. expect(throwable).to.throw(
  707. 'The property "foo" of the model "model" must have ' +
  708. 'a String, but Array given.',
  709. );
  710. });
  711. it('throws an error if an object given', function () {
  712. const S = new Schema();
  713. S.defineModel({
  714. name: 'model',
  715. datasource: 'datasource',
  716. properties: {
  717. foo: {
  718. type: DataType.STRING,
  719. },
  720. },
  721. });
  722. const throwable = () =>
  723. S.getService(ModelDataValidator).validate('model', {
  724. foo: {},
  725. });
  726. expect(throwable).to.throw(
  727. 'The property "foo" of the model "model" must have ' +
  728. 'a String, but Object given.',
  729. );
  730. });
  731. });
  732. });
  733. describe('DataType.NUMBER', function () {
  734. describe('ShortPropertyDefinition', function () {
  735. it('does not throw an error if an undefined given', function () {
  736. const S = new Schema();
  737. S.defineModel({
  738. name: 'model',
  739. datasource: 'datasource',
  740. properties: {
  741. foo: DataType.NUMBER,
  742. },
  743. });
  744. S.getService(ModelDataValidator).validate('model', {
  745. foo: undefined,
  746. });
  747. });
  748. it('does not throw an error if a null given', function () {
  749. const S = new Schema();
  750. S.defineModel({
  751. name: 'model',
  752. datasource: 'datasource',
  753. properties: {
  754. foo: DataType.NUMBER,
  755. },
  756. });
  757. S.getService(ModelDataValidator).validate('model', {
  758. foo: null,
  759. });
  760. });
  761. it('throws an error if a string given', function () {
  762. const S = new Schema();
  763. S.defineModel({
  764. name: 'model',
  765. datasource: 'datasource',
  766. properties: {
  767. foo: DataType.NUMBER,
  768. },
  769. });
  770. const throwable = () =>
  771. S.getService(ModelDataValidator).validate('model', {
  772. foo: 'bar',
  773. });
  774. expect(throwable).to.throw(
  775. 'The property "foo" of the model "model" must have ' +
  776. 'a Number, but String given.',
  777. );
  778. });
  779. it('does not throw an error if a number given', function () {
  780. const S = new Schema();
  781. S.defineModel({
  782. name: 'model',
  783. datasource: 'datasource',
  784. properties: {
  785. foo: DataType.NUMBER,
  786. },
  787. });
  788. S.getService(ModelDataValidator).validate('model', {
  789. foo: 10,
  790. });
  791. });
  792. it('throws an error if true given', function () {
  793. const S = new Schema();
  794. S.defineModel({
  795. name: 'model',
  796. datasource: 'datasource',
  797. properties: {
  798. foo: DataType.NUMBER,
  799. },
  800. });
  801. const throwable = () =>
  802. S.getService(ModelDataValidator).validate('model', {
  803. foo: true,
  804. });
  805. expect(throwable).to.throw(
  806. 'The property "foo" of the model "model" must have ' +
  807. 'a Number, but Boolean given.',
  808. );
  809. });
  810. it('throws an error if false given', function () {
  811. const S = new Schema();
  812. S.defineModel({
  813. name: 'model',
  814. datasource: 'datasource',
  815. properties: {
  816. foo: DataType.NUMBER,
  817. },
  818. });
  819. const throwable = () =>
  820. S.getService(ModelDataValidator).validate('model', {
  821. foo: false,
  822. });
  823. expect(throwable).to.throw(
  824. 'The property "foo" of the model "model" must have ' +
  825. 'a Number, but Boolean given.',
  826. );
  827. });
  828. it('throws an error if an array given', function () {
  829. const S = new Schema();
  830. S.defineModel({
  831. name: 'model',
  832. datasource: 'datasource',
  833. properties: {
  834. foo: DataType.NUMBER,
  835. },
  836. });
  837. const throwable = () =>
  838. S.getService(ModelDataValidator).validate('model', {
  839. foo: [],
  840. });
  841. expect(throwable).to.throw(
  842. 'The property "foo" of the model "model" must have ' +
  843. 'a Number, but Array given.',
  844. );
  845. });
  846. it('throws an error if an object given', function () {
  847. const S = new Schema();
  848. S.defineModel({
  849. name: 'model',
  850. datasource: 'datasource',
  851. properties: {
  852. foo: DataType.NUMBER,
  853. },
  854. });
  855. const throwable = () =>
  856. S.getService(ModelDataValidator).validate('model', {
  857. foo: {},
  858. });
  859. expect(throwable).to.throw(
  860. 'The property "foo" of the model "model" must have ' +
  861. 'a Number, but Object given.',
  862. );
  863. });
  864. });
  865. describe('FullPropertyDefinition', function () {
  866. it('does not throw an error if an undefined given', function () {
  867. const S = new Schema();
  868. S.defineModel({
  869. name: 'model',
  870. datasource: 'datasource',
  871. properties: {
  872. foo: {
  873. type: DataType.NUMBER,
  874. },
  875. },
  876. });
  877. S.getService(ModelDataValidator).validate('model', {
  878. foo: undefined,
  879. });
  880. });
  881. it('does not throw an error if a null given', function () {
  882. const S = new Schema();
  883. S.defineModel({
  884. name: 'model',
  885. datasource: 'datasource',
  886. properties: {
  887. foo: {
  888. type: DataType.NUMBER,
  889. },
  890. },
  891. });
  892. S.getService(ModelDataValidator).validate('model', {
  893. foo: null,
  894. });
  895. });
  896. it('throws an error if a string given', function () {
  897. const S = new Schema();
  898. S.defineModel({
  899. name: 'model',
  900. datasource: 'datasource',
  901. properties: {
  902. foo: {
  903. type: DataType.NUMBER,
  904. },
  905. },
  906. });
  907. const throwable = () =>
  908. S.getService(ModelDataValidator).validate('model', {
  909. foo: 'bar',
  910. });
  911. expect(throwable).to.throw(
  912. 'The property "foo" of the model "model" must have ' +
  913. 'a Number, but String given.',
  914. );
  915. });
  916. it('does not throw an error if a number given', function () {
  917. const S = new Schema();
  918. S.defineModel({
  919. name: 'model',
  920. datasource: 'datasource',
  921. properties: {
  922. foo: {
  923. type: DataType.NUMBER,
  924. },
  925. },
  926. });
  927. S.getService(ModelDataValidator).validate('model', {
  928. foo: 10,
  929. });
  930. });
  931. it('throws an error if true given', function () {
  932. const S = new Schema();
  933. S.defineModel({
  934. name: 'model',
  935. datasource: 'datasource',
  936. properties: {
  937. foo: {
  938. type: DataType.NUMBER,
  939. },
  940. },
  941. });
  942. const throwable = () =>
  943. S.getService(ModelDataValidator).validate('model', {
  944. foo: true,
  945. });
  946. expect(throwable).to.throw(
  947. 'The property "foo" of the model "model" must have ' +
  948. 'a Number, but Boolean given.',
  949. );
  950. });
  951. it('throws an error if false given', function () {
  952. const S = new Schema();
  953. S.defineModel({
  954. name: 'model',
  955. datasource: 'datasource',
  956. properties: {
  957. foo: {
  958. type: DataType.NUMBER,
  959. },
  960. },
  961. });
  962. const throwable = () =>
  963. S.getService(ModelDataValidator).validate('model', {
  964. foo: false,
  965. });
  966. expect(throwable).to.throw(
  967. 'The property "foo" of the model "model" must have ' +
  968. 'a Number, but Boolean given.',
  969. );
  970. });
  971. it('throws an error if an array given', function () {
  972. const S = new Schema();
  973. S.defineModel({
  974. name: 'model',
  975. datasource: 'datasource',
  976. properties: {
  977. foo: {
  978. type: DataType.NUMBER,
  979. },
  980. },
  981. });
  982. const throwable = () =>
  983. S.getService(ModelDataValidator).validate('model', {
  984. foo: [],
  985. });
  986. expect(throwable).to.throw(
  987. 'The property "foo" of the model "model" must have ' +
  988. 'a Number, but Array given.',
  989. );
  990. });
  991. it('throws an error if an object given', function () {
  992. const S = new Schema();
  993. S.defineModel({
  994. name: 'model',
  995. datasource: 'datasource',
  996. properties: {
  997. foo: {
  998. type: DataType.NUMBER,
  999. },
  1000. },
  1001. });
  1002. const throwable = () =>
  1003. S.getService(ModelDataValidator).validate('model', {
  1004. foo: {},
  1005. });
  1006. expect(throwable).to.throw(
  1007. 'The property "foo" of the model "model" must have ' +
  1008. 'a Number, but Object given.',
  1009. );
  1010. });
  1011. });
  1012. });
  1013. describe('DataType.BOOLEAN', function () {
  1014. describe('ShortPropertyDefinition', function () {
  1015. it('does not throw an error if an undefined given', function () {
  1016. const S = new Schema();
  1017. S.defineModel({
  1018. name: 'model',
  1019. datasource: 'datasource',
  1020. properties: {
  1021. foo: DataType.BOOLEAN,
  1022. },
  1023. });
  1024. S.getService(ModelDataValidator).validate('model', {
  1025. foo: undefined,
  1026. });
  1027. });
  1028. it('does not throw an error if a null given', function () {
  1029. const S = new Schema();
  1030. S.defineModel({
  1031. name: 'model',
  1032. datasource: 'datasource',
  1033. properties: {
  1034. foo: DataType.BOOLEAN,
  1035. },
  1036. });
  1037. S.getService(ModelDataValidator).validate('model', {
  1038. foo: null,
  1039. });
  1040. });
  1041. it('throws an error if a string given', function () {
  1042. const S = new Schema();
  1043. S.defineModel({
  1044. name: 'model',
  1045. datasource: 'datasource',
  1046. properties: {
  1047. foo: DataType.BOOLEAN,
  1048. },
  1049. });
  1050. const throwable = () =>
  1051. S.getService(ModelDataValidator).validate('model', {
  1052. foo: 'bar',
  1053. });
  1054. expect(throwable).to.throw(
  1055. 'The property "foo" of the model "model" must have ' +
  1056. 'a Boolean, but String given.',
  1057. );
  1058. });
  1059. it('throws an error if a number given', function () {
  1060. const S = new Schema();
  1061. S.defineModel({
  1062. name: 'model',
  1063. datasource: 'datasource',
  1064. properties: {
  1065. foo: DataType.BOOLEAN,
  1066. },
  1067. });
  1068. const throwable = () =>
  1069. S.getService(ModelDataValidator).validate('model', {
  1070. foo: 10,
  1071. });
  1072. expect(throwable).to.throw(
  1073. 'The property "foo" of the model "model" must have ' +
  1074. 'a Boolean, but Number given.',
  1075. );
  1076. });
  1077. it('does not throw an error if true given', function () {
  1078. const S = new Schema();
  1079. S.defineModel({
  1080. name: 'model',
  1081. datasource: 'datasource',
  1082. properties: {
  1083. foo: DataType.BOOLEAN,
  1084. },
  1085. });
  1086. S.getService(ModelDataValidator).validate('model', {
  1087. foo: true,
  1088. });
  1089. });
  1090. it('does not throw an error if false given', function () {
  1091. const S = new Schema();
  1092. S.defineModel({
  1093. name: 'model',
  1094. datasource: 'datasource',
  1095. properties: {
  1096. foo: DataType.BOOLEAN,
  1097. },
  1098. });
  1099. S.getService(ModelDataValidator).validate('model', {
  1100. foo: false,
  1101. });
  1102. });
  1103. it('throws an error if an array given', function () {
  1104. const S = new Schema();
  1105. S.defineModel({
  1106. name: 'model',
  1107. datasource: 'datasource',
  1108. properties: {
  1109. foo: DataType.BOOLEAN,
  1110. },
  1111. });
  1112. const throwable = () =>
  1113. S.getService(ModelDataValidator).validate('model', {
  1114. foo: [],
  1115. });
  1116. expect(throwable).to.throw(
  1117. 'The property "foo" of the model "model" must have ' +
  1118. 'a Boolean, but Array given.',
  1119. );
  1120. });
  1121. it('throws an error if an object given', function () {
  1122. const S = new Schema();
  1123. S.defineModel({
  1124. name: 'model',
  1125. datasource: 'datasource',
  1126. properties: {
  1127. foo: DataType.BOOLEAN,
  1128. },
  1129. });
  1130. const throwable = () =>
  1131. S.getService(ModelDataValidator).validate('model', {
  1132. foo: {},
  1133. });
  1134. expect(throwable).to.throw(
  1135. 'The property "foo" of the model "model" must have ' +
  1136. 'a Boolean, but Object given.',
  1137. );
  1138. });
  1139. });
  1140. describe('FullPropertyDefinition', function () {
  1141. it('does not throw an error if an undefined given', function () {
  1142. const S = new Schema();
  1143. S.defineModel({
  1144. name: 'model',
  1145. datasource: 'datasource',
  1146. properties: {
  1147. foo: {
  1148. type: DataType.BOOLEAN,
  1149. },
  1150. },
  1151. });
  1152. S.getService(ModelDataValidator).validate('model', {
  1153. foo: undefined,
  1154. });
  1155. });
  1156. it('does not throw an error if a null given', function () {
  1157. const S = new Schema();
  1158. S.defineModel({
  1159. name: 'model',
  1160. datasource: 'datasource',
  1161. properties: {
  1162. foo: {
  1163. type: DataType.BOOLEAN,
  1164. },
  1165. },
  1166. });
  1167. S.getService(ModelDataValidator).validate('model', {
  1168. foo: null,
  1169. });
  1170. });
  1171. it('throws an error if a string given', function () {
  1172. const S = new Schema();
  1173. S.defineModel({
  1174. name: 'model',
  1175. datasource: 'datasource',
  1176. properties: {
  1177. foo: {
  1178. type: DataType.BOOLEAN,
  1179. },
  1180. },
  1181. });
  1182. const throwable = () =>
  1183. S.getService(ModelDataValidator).validate('model', {
  1184. foo: 'bar',
  1185. });
  1186. expect(throwable).to.throw(
  1187. 'The property "foo" of the model "model" must have ' +
  1188. 'a Boolean, but String given.',
  1189. );
  1190. });
  1191. it('throws an error if a number given', function () {
  1192. const S = new Schema();
  1193. S.defineModel({
  1194. name: 'model',
  1195. datasource: 'datasource',
  1196. properties: {
  1197. foo: {
  1198. type: DataType.BOOLEAN,
  1199. },
  1200. },
  1201. });
  1202. const throwable = () =>
  1203. S.getService(ModelDataValidator).validate('model', {
  1204. foo: 10,
  1205. });
  1206. expect(throwable).to.throw(
  1207. 'The property "foo" of the model "model" must have ' +
  1208. 'a Boolean, but Number given.',
  1209. );
  1210. });
  1211. it('does not throw an error if true given', function () {
  1212. const S = new Schema();
  1213. S.defineModel({
  1214. name: 'model',
  1215. datasource: 'datasource',
  1216. properties: {
  1217. foo: {
  1218. type: DataType.BOOLEAN,
  1219. },
  1220. },
  1221. });
  1222. S.getService(ModelDataValidator).validate('model', {
  1223. foo: true,
  1224. });
  1225. });
  1226. it('does not throw an error if false given', function () {
  1227. const S = new Schema();
  1228. S.defineModel({
  1229. name: 'model',
  1230. datasource: 'datasource',
  1231. properties: {
  1232. foo: {
  1233. type: DataType.BOOLEAN,
  1234. },
  1235. },
  1236. });
  1237. S.getService(ModelDataValidator).validate('model', {
  1238. foo: false,
  1239. });
  1240. });
  1241. it('throws an error if an array given', function () {
  1242. const S = new Schema();
  1243. S.defineModel({
  1244. name: 'model',
  1245. datasource: 'datasource',
  1246. properties: {
  1247. foo: {
  1248. type: DataType.BOOLEAN,
  1249. },
  1250. },
  1251. });
  1252. const throwable = () =>
  1253. S.getService(ModelDataValidator).validate('model', {
  1254. foo: [],
  1255. });
  1256. expect(throwable).to.throw(
  1257. 'The property "foo" of the model "model" must have ' +
  1258. 'a Boolean, but Array given.',
  1259. );
  1260. });
  1261. it('throws an error if an object given', function () {
  1262. const S = new Schema();
  1263. S.defineModel({
  1264. name: 'model',
  1265. datasource: 'datasource',
  1266. properties: {
  1267. foo: {
  1268. type: DataType.BOOLEAN,
  1269. },
  1270. },
  1271. });
  1272. const throwable = () =>
  1273. S.getService(ModelDataValidator).validate('model', {
  1274. foo: {},
  1275. });
  1276. expect(throwable).to.throw(
  1277. 'The property "foo" of the model "model" must have ' +
  1278. 'a Boolean, but Object given.',
  1279. );
  1280. });
  1281. });
  1282. });
  1283. describe('DataType.ARRAY', function () {
  1284. describe('ShortPropertyDefinition', function () {
  1285. it('does not throw an error if an undefined given', function () {
  1286. const S = new Schema();
  1287. S.defineModel({
  1288. name: 'model',
  1289. datasource: 'datasource',
  1290. properties: {
  1291. foo: DataType.ARRAY,
  1292. },
  1293. });
  1294. S.getService(ModelDataValidator).validate('model', {
  1295. foo: undefined,
  1296. });
  1297. });
  1298. it('does not throw an error if a null given', function () {
  1299. const S = new Schema();
  1300. S.defineModel({
  1301. name: 'model',
  1302. datasource: 'datasource',
  1303. properties: {
  1304. foo: DataType.ARRAY,
  1305. },
  1306. });
  1307. S.getService(ModelDataValidator).validate('model', {
  1308. foo: null,
  1309. });
  1310. });
  1311. it('throws an error if a string given', function () {
  1312. const S = new Schema();
  1313. S.defineModel({
  1314. name: 'model',
  1315. datasource: 'datasource',
  1316. properties: {
  1317. foo: DataType.ARRAY,
  1318. },
  1319. });
  1320. const throwable = () =>
  1321. S.getService(ModelDataValidator).validate('model', {
  1322. foo: 'bar',
  1323. });
  1324. expect(throwable).to.throw(
  1325. 'The property "foo" of the model "model" must have ' +
  1326. 'an Array, but String given.',
  1327. );
  1328. });
  1329. it('throws an error if a number given', function () {
  1330. const S = new Schema();
  1331. S.defineModel({
  1332. name: 'model',
  1333. datasource: 'datasource',
  1334. properties: {
  1335. foo: DataType.ARRAY,
  1336. },
  1337. });
  1338. const throwable = () =>
  1339. S.getService(ModelDataValidator).validate('model', {
  1340. foo: 10,
  1341. });
  1342. expect(throwable).to.throw(
  1343. 'The property "foo" of the model "model" must have ' +
  1344. 'an Array, but Number given.',
  1345. );
  1346. });
  1347. it('throws an error if true given', function () {
  1348. const S = new Schema();
  1349. S.defineModel({
  1350. name: 'model',
  1351. datasource: 'datasource',
  1352. properties: {
  1353. foo: DataType.ARRAY,
  1354. },
  1355. });
  1356. const throwable = () =>
  1357. S.getService(ModelDataValidator).validate('model', {
  1358. foo: true,
  1359. });
  1360. expect(throwable).to.throw(
  1361. 'The property "foo" of the model "model" must have ' +
  1362. 'an Array, but Boolean given.',
  1363. );
  1364. });
  1365. it('throws an error if false given', function () {
  1366. const S = new Schema();
  1367. S.defineModel({
  1368. name: 'model',
  1369. datasource: 'datasource',
  1370. properties: {
  1371. foo: DataType.ARRAY,
  1372. },
  1373. });
  1374. const throwable = () =>
  1375. S.getService(ModelDataValidator).validate('model', {
  1376. foo: false,
  1377. });
  1378. expect(throwable).to.throw(
  1379. 'The property "foo" of the model "model" must have ' +
  1380. 'an Array, but Boolean given.',
  1381. );
  1382. });
  1383. it('does not throw an error if an array given', function () {
  1384. const S = new Schema();
  1385. S.defineModel({
  1386. name: 'model',
  1387. datasource: 'datasource',
  1388. properties: {
  1389. foo: DataType.ARRAY,
  1390. },
  1391. });
  1392. S.getService(ModelDataValidator).validate('model', {
  1393. foo: [],
  1394. });
  1395. });
  1396. it('throws an error if an object given', function () {
  1397. const S = new Schema();
  1398. S.defineModel({
  1399. name: 'model',
  1400. datasource: 'datasource',
  1401. properties: {
  1402. foo: DataType.ARRAY,
  1403. },
  1404. });
  1405. const throwable = () =>
  1406. S.getService(ModelDataValidator).validate('model', {
  1407. foo: {},
  1408. });
  1409. expect(throwable).to.throw(
  1410. 'The property "foo" of the model "model" must have ' +
  1411. 'an Array, but Object given.',
  1412. );
  1413. });
  1414. });
  1415. describe('FullPropertyDefinition', function () {
  1416. it('does not throw an error if an undefined given', function () {
  1417. const S = new Schema();
  1418. S.defineModel({
  1419. name: 'model',
  1420. datasource: 'datasource',
  1421. properties: {
  1422. foo: {
  1423. type: DataType.ARRAY,
  1424. },
  1425. },
  1426. });
  1427. S.getService(ModelDataValidator).validate('model', {
  1428. foo: undefined,
  1429. });
  1430. });
  1431. it('does not throw an error if a null given', function () {
  1432. const S = new Schema();
  1433. S.defineModel({
  1434. name: 'model',
  1435. datasource: 'datasource',
  1436. properties: {
  1437. foo: {
  1438. type: DataType.ARRAY,
  1439. },
  1440. },
  1441. });
  1442. S.getService(ModelDataValidator).validate('model', {
  1443. foo: null,
  1444. });
  1445. });
  1446. it('throws an error if a string given', function () {
  1447. const S = new Schema();
  1448. S.defineModel({
  1449. name: 'model',
  1450. datasource: 'datasource',
  1451. properties: {
  1452. foo: {
  1453. type: DataType.ARRAY,
  1454. },
  1455. },
  1456. });
  1457. const throwable = () =>
  1458. S.getService(ModelDataValidator).validate('model', {
  1459. foo: 'bar',
  1460. });
  1461. expect(throwable).to.throw(
  1462. 'The property "foo" of the model "model" must have ' +
  1463. 'an Array, but String given.',
  1464. );
  1465. });
  1466. it('throws an error if a number given', function () {
  1467. const S = new Schema();
  1468. S.defineModel({
  1469. name: 'model',
  1470. datasource: 'datasource',
  1471. properties: {
  1472. foo: {
  1473. type: DataType.ARRAY,
  1474. },
  1475. },
  1476. });
  1477. const throwable = () =>
  1478. S.getService(ModelDataValidator).validate('model', {
  1479. foo: 10,
  1480. });
  1481. expect(throwable).to.throw(
  1482. 'The property "foo" of the model "model" must have ' +
  1483. 'an Array, but Number given.',
  1484. );
  1485. });
  1486. it('throws an error if true given', function () {
  1487. const S = new Schema();
  1488. S.defineModel({
  1489. name: 'model',
  1490. datasource: 'datasource',
  1491. properties: {
  1492. foo: {
  1493. type: DataType.ARRAY,
  1494. },
  1495. },
  1496. });
  1497. const throwable = () =>
  1498. S.getService(ModelDataValidator).validate('model', {
  1499. foo: true,
  1500. });
  1501. expect(throwable).to.throw(
  1502. 'The property "foo" of the model "model" must have ' +
  1503. 'an Array, but Boolean given.',
  1504. );
  1505. });
  1506. it('throws an error if false given', function () {
  1507. const S = new Schema();
  1508. S.defineModel({
  1509. name: 'model',
  1510. datasource: 'datasource',
  1511. properties: {
  1512. foo: {
  1513. type: DataType.ARRAY,
  1514. },
  1515. },
  1516. });
  1517. const throwable = () =>
  1518. S.getService(ModelDataValidator).validate('model', {
  1519. foo: false,
  1520. });
  1521. expect(throwable).to.throw(
  1522. 'The property "foo" of the model "model" must have ' +
  1523. 'an Array, but Boolean given.',
  1524. );
  1525. });
  1526. it('does not throw an error if an array given', function () {
  1527. const S = new Schema();
  1528. S.defineModel({
  1529. name: 'model',
  1530. datasource: 'datasource',
  1531. properties: {
  1532. foo: {
  1533. type: DataType.ARRAY,
  1534. },
  1535. },
  1536. });
  1537. S.getService(ModelDataValidator).validate('model', {
  1538. foo: [],
  1539. });
  1540. });
  1541. it('throws an error if an object given', function () {
  1542. const S = new Schema();
  1543. S.defineModel({
  1544. name: 'model',
  1545. datasource: 'datasource',
  1546. properties: {
  1547. foo: {
  1548. type: DataType.ARRAY,
  1549. },
  1550. },
  1551. });
  1552. const throwable = () =>
  1553. S.getService(ModelDataValidator).validate('model', {
  1554. foo: {},
  1555. });
  1556. expect(throwable).to.throw(
  1557. 'The property "foo" of the model "model" must have ' +
  1558. 'an Array, but Object given.',
  1559. );
  1560. });
  1561. describe('the "model" option', function () {
  1562. it('throws an error when the given object element has an invalid model', function () {
  1563. const S = new Schema();
  1564. S.defineModel({
  1565. name: 'modelA',
  1566. properties: {
  1567. foo: DataType.STRING,
  1568. },
  1569. });
  1570. S.defineModel({
  1571. name: 'modelB',
  1572. datasource: 'datasource',
  1573. properties: {
  1574. bar: {
  1575. type: DataType.ARRAY,
  1576. itemType: DataType.OBJECT,
  1577. model: 'modelA',
  1578. },
  1579. },
  1580. });
  1581. const throwable = () =>
  1582. S.getService(ModelDataValidator).validate('modelB', {
  1583. bar: [{foo: 10}],
  1584. });
  1585. expect(throwable).to.throw(
  1586. 'The property "foo" of the model "modelA" must have ' +
  1587. 'a String, but Number given.',
  1588. );
  1589. });
  1590. it('does not throw an error when the given object element has a valid model', function () {
  1591. const S = new Schema();
  1592. S.defineModel({
  1593. name: 'modelA',
  1594. properties: {
  1595. foo: DataType.STRING,
  1596. },
  1597. });
  1598. S.defineModel({
  1599. name: 'modelB',
  1600. datasource: 'datasource',
  1601. properties: {
  1602. bar: {
  1603. type: DataType.ARRAY,
  1604. itemType: DataType.OBJECT,
  1605. model: 'modelA',
  1606. },
  1607. },
  1608. });
  1609. S.getService(ModelDataValidator).validate('modelB', {
  1610. bar: [{foo: '10'}],
  1611. });
  1612. });
  1613. });
  1614. });
  1615. });
  1616. describe('DataType.OBJECT', function () {
  1617. describe('ShortPropertyDefinition', function () {
  1618. it('does not throw an error if an undefined given', function () {
  1619. const S = new Schema();
  1620. S.defineModel({
  1621. name: 'model',
  1622. datasource: 'datasource',
  1623. properties: {
  1624. foo: DataType.OBJECT,
  1625. },
  1626. });
  1627. S.getService(ModelDataValidator).validate('model', {
  1628. foo: undefined,
  1629. });
  1630. });
  1631. it('does not throw an error if a null given', function () {
  1632. const S = new Schema();
  1633. S.defineModel({
  1634. name: 'model',
  1635. datasource: 'datasource',
  1636. properties: {
  1637. foo: DataType.OBJECT,
  1638. },
  1639. });
  1640. S.getService(ModelDataValidator).validate('model', {
  1641. foo: null,
  1642. });
  1643. });
  1644. it('throws an error if a string given', function () {
  1645. const S = new Schema();
  1646. S.defineModel({
  1647. name: 'model',
  1648. datasource: 'datasource',
  1649. properties: {
  1650. foo: DataType.OBJECT,
  1651. },
  1652. });
  1653. const throwable = () =>
  1654. S.getService(ModelDataValidator).validate('model', {
  1655. foo: 'bar',
  1656. });
  1657. expect(throwable).to.throw(
  1658. 'The property "foo" of the model "model" must have ' +
  1659. 'an Object, but String given.',
  1660. );
  1661. });
  1662. it('throws an error if a number given', function () {
  1663. const S = new Schema();
  1664. S.defineModel({
  1665. name: 'model',
  1666. datasource: 'datasource',
  1667. properties: {
  1668. foo: DataType.OBJECT,
  1669. },
  1670. });
  1671. const throwable = () =>
  1672. S.getService(ModelDataValidator).validate('model', {
  1673. foo: 10,
  1674. });
  1675. expect(throwable).to.throw(
  1676. 'The property "foo" of the model "model" must have ' +
  1677. 'an Object, but Number given.',
  1678. );
  1679. });
  1680. it('throws an error if true given', function () {
  1681. const S = new Schema();
  1682. S.defineModel({
  1683. name: 'model',
  1684. datasource: 'datasource',
  1685. properties: {
  1686. foo: DataType.OBJECT,
  1687. },
  1688. });
  1689. const throwable = () =>
  1690. S.getService(ModelDataValidator).validate('model', {
  1691. foo: true,
  1692. });
  1693. expect(throwable).to.throw(
  1694. 'The property "foo" of the model "model" must have ' +
  1695. 'an Object, but Boolean given.',
  1696. );
  1697. });
  1698. it('throws an error if false given', function () {
  1699. const S = new Schema();
  1700. S.defineModel({
  1701. name: 'model',
  1702. datasource: 'datasource',
  1703. properties: {
  1704. foo: DataType.OBJECT,
  1705. },
  1706. });
  1707. const throwable = () =>
  1708. S.getService(ModelDataValidator).validate('model', {
  1709. foo: false,
  1710. });
  1711. expect(throwable).to.throw(
  1712. 'The property "foo" of the model "model" must have ' +
  1713. 'an Object, but Boolean given.',
  1714. );
  1715. });
  1716. it('throws an error if an array given', function () {
  1717. const S = new Schema();
  1718. S.defineModel({
  1719. name: 'model',
  1720. datasource: 'datasource',
  1721. properties: {
  1722. foo: DataType.OBJECT,
  1723. },
  1724. });
  1725. const throwable = () =>
  1726. S.getService(ModelDataValidator).validate('model', {
  1727. foo: [],
  1728. });
  1729. expect(throwable).to.throw(
  1730. 'The property "foo" of the model "model" must have ' +
  1731. 'an Object, but Array given.',
  1732. );
  1733. });
  1734. it('does not throw an error if an object given', function () {
  1735. const S = new Schema();
  1736. S.defineModel({
  1737. name: 'model',
  1738. datasource: 'datasource',
  1739. properties: {
  1740. foo: DataType.OBJECT,
  1741. },
  1742. });
  1743. S.getService(ModelDataValidator).validate('model', {
  1744. foo: {},
  1745. });
  1746. });
  1747. });
  1748. describe('FullPropertyDefinition', function () {
  1749. it('does not throw an error if an undefined given', function () {
  1750. const S = new Schema();
  1751. S.defineModel({
  1752. name: 'model',
  1753. datasource: 'datasource',
  1754. properties: {
  1755. foo: {
  1756. type: DataType.OBJECT,
  1757. },
  1758. },
  1759. });
  1760. S.getService(ModelDataValidator).validate('model', {
  1761. foo: undefined,
  1762. });
  1763. });
  1764. it('does not throw an error if a null given', function () {
  1765. const S = new Schema();
  1766. S.defineModel({
  1767. name: 'model',
  1768. datasource: 'datasource',
  1769. properties: {
  1770. foo: {
  1771. type: DataType.OBJECT,
  1772. },
  1773. },
  1774. });
  1775. S.getService(ModelDataValidator).validate('model', {
  1776. foo: null,
  1777. });
  1778. });
  1779. it('throws an error if a string given', function () {
  1780. const S = new Schema();
  1781. S.defineModel({
  1782. name: 'model',
  1783. datasource: 'datasource',
  1784. properties: {
  1785. foo: {
  1786. type: DataType.OBJECT,
  1787. },
  1788. },
  1789. });
  1790. const throwable = () =>
  1791. S.getService(ModelDataValidator).validate('model', {
  1792. foo: 'bar',
  1793. });
  1794. expect(throwable).to.throw(
  1795. 'The property "foo" of the model "model" must have ' +
  1796. 'an Object, but String given.',
  1797. );
  1798. });
  1799. it('throws an error if a number given', function () {
  1800. const S = new Schema();
  1801. S.defineModel({
  1802. name: 'model',
  1803. datasource: 'datasource',
  1804. properties: {
  1805. foo: {
  1806. type: DataType.OBJECT,
  1807. },
  1808. },
  1809. });
  1810. const throwable = () =>
  1811. S.getService(ModelDataValidator).validate('model', {
  1812. foo: 10,
  1813. });
  1814. expect(throwable).to.throw(
  1815. 'The property "foo" of the model "model" must have ' +
  1816. 'an Object, but Number given.',
  1817. );
  1818. });
  1819. it('throws an error if true given', function () {
  1820. const S = new Schema();
  1821. S.defineModel({
  1822. name: 'model',
  1823. datasource: 'datasource',
  1824. properties: {
  1825. foo: {
  1826. type: DataType.OBJECT,
  1827. },
  1828. },
  1829. });
  1830. const throwable = () =>
  1831. S.getService(ModelDataValidator).validate('model', {
  1832. foo: true,
  1833. });
  1834. expect(throwable).to.throw(
  1835. 'The property "foo" of the model "model" must have ' +
  1836. 'an Object, but Boolean given.',
  1837. );
  1838. });
  1839. it('throws an error if false given', function () {
  1840. const S = new Schema();
  1841. S.defineModel({
  1842. name: 'model',
  1843. datasource: 'datasource',
  1844. properties: {
  1845. foo: {
  1846. type: DataType.OBJECT,
  1847. },
  1848. },
  1849. });
  1850. const throwable = () =>
  1851. S.getService(ModelDataValidator).validate('model', {
  1852. foo: false,
  1853. });
  1854. expect(throwable).to.throw(
  1855. 'The property "foo" of the model "model" must have ' +
  1856. 'an Object, but Boolean given.',
  1857. );
  1858. });
  1859. it('throws an error if an array given', function () {
  1860. const S = new Schema();
  1861. S.defineModel({
  1862. name: 'model',
  1863. datasource: 'datasource',
  1864. properties: {
  1865. foo: {
  1866. type: DataType.OBJECT,
  1867. },
  1868. },
  1869. });
  1870. const throwable = () =>
  1871. S.getService(ModelDataValidator).validate('model', {
  1872. foo: [],
  1873. });
  1874. expect(throwable).to.throw(
  1875. 'The property "foo" of the model "model" must have ' +
  1876. 'an Object, but Array given.',
  1877. );
  1878. });
  1879. it('does not throw an error if an object given', function () {
  1880. const S = new Schema();
  1881. S.defineModel({
  1882. name: 'model',
  1883. datasource: 'datasource',
  1884. properties: {
  1885. foo: {
  1886. type: DataType.OBJECT,
  1887. },
  1888. },
  1889. });
  1890. S.getService(ModelDataValidator).validate('model', {
  1891. foo: {},
  1892. });
  1893. });
  1894. describe('the "model" option', function () {
  1895. it('throws an error when the given object has an invalid model', function () {
  1896. const S = new Schema();
  1897. S.defineModel({
  1898. name: 'modelA',
  1899. properties: {
  1900. foo: DataType.STRING,
  1901. },
  1902. });
  1903. S.defineModel({
  1904. name: 'modelB',
  1905. datasource: 'datasource',
  1906. properties: {
  1907. bar: {
  1908. type: DataType.OBJECT,
  1909. model: 'modelA',
  1910. },
  1911. },
  1912. });
  1913. const throwable = () =>
  1914. S.getService(ModelDataValidator).validate('modelB', {
  1915. bar: {foo: 10},
  1916. });
  1917. expect(throwable).to.throw(
  1918. 'The property "foo" of the model "modelA" must have ' +
  1919. 'a String, but Number given.',
  1920. );
  1921. });
  1922. it('does not throw an error when the given object has a valid model', function () {
  1923. const S = new Schema();
  1924. S.defineModel({
  1925. name: 'modelA',
  1926. properties: {
  1927. foo: DataType.STRING,
  1928. },
  1929. });
  1930. S.defineModel({
  1931. name: 'modelB',
  1932. datasource: 'datasource',
  1933. properties: {
  1934. bar: {
  1935. type: DataType.OBJECT,
  1936. model: 'modelA',
  1937. },
  1938. },
  1939. });
  1940. S.getService(ModelDataValidator).validate('modelB', {
  1941. bar: {foo: '10'},
  1942. });
  1943. });
  1944. });
  1945. });
  1946. });
  1947. });
  1948. describe('validate by property validators', function () {
  1949. it('skips validation for an empty value', function () {
  1950. const S = new Schema();
  1951. S.getService(PropertyValidatorRegistry).addValidator(
  1952. 'myValidator',
  1953. () => false,
  1954. );
  1955. S.defineModel({
  1956. name: 'model',
  1957. properties: {
  1958. foo: {
  1959. type: DataType.STRING,
  1960. validate: 'myValidator',
  1961. },
  1962. },
  1963. });
  1964. S.getService(EmptyValuesDefiner).setEmptyValuesOf(DataType.STRING, [5]);
  1965. S.getService(ModelDataValidator).validate('model', {foo: 5});
  1966. });
  1967. describe('the option "validate" with the string value', function () {
  1968. it('does not validate a property value if it is not provided', function () {
  1969. const S = new Schema();
  1970. S.getService(PropertyValidatorRegistry).addValidator(
  1971. 'myValidator',
  1972. () => false,
  1973. );
  1974. S.defineModel({
  1975. name: 'model',
  1976. properties: {
  1977. foo: {
  1978. type: DataType.ANY,
  1979. validate: 'myValidator',
  1980. },
  1981. },
  1982. });
  1983. const validator = S.getService(ModelDataValidator);
  1984. validator.validate('model', {});
  1985. });
  1986. it('does not validate undefined and null values', function () {
  1987. const S = new Schema();
  1988. S.getService(PropertyValidatorRegistry).addValidator(
  1989. 'myValidator',
  1990. () => false,
  1991. );
  1992. S.defineModel({
  1993. name: 'model',
  1994. properties: {
  1995. foo: {
  1996. type: DataType.ANY,
  1997. validate: 'myValidator',
  1998. },
  1999. },
  2000. });
  2001. const validator = S.getService(ModelDataValidator);
  2002. validator.validate('model', {foo: undefined});
  2003. validator.validate('model', {foo: null});
  2004. });
  2005. it('throws an error from the validator', function () {
  2006. const myValidator = function () {
  2007. throw Error('My error');
  2008. };
  2009. const S = new Schema();
  2010. S.getService(PropertyValidatorRegistry).addValidator(
  2011. 'myValidator',
  2012. myValidator,
  2013. );
  2014. S.defineModel({
  2015. name: 'model',
  2016. properties: {
  2017. foo: {
  2018. type: DataType.ANY,
  2019. validate: 'myValidator',
  2020. },
  2021. },
  2022. });
  2023. const throwable = () =>
  2024. S.getService(ModelDataValidator).validate('model', {
  2025. foo: 'test',
  2026. });
  2027. expect(throwable).to.throw('My error');
  2028. });
  2029. it('allows the given value if the validator returns true', function () {
  2030. const S = new Schema();
  2031. S.getService(PropertyValidatorRegistry).addValidator(
  2032. 'myValidator',
  2033. () => true,
  2034. );
  2035. S.defineModel({
  2036. name: 'model',
  2037. properties: {
  2038. foo: {
  2039. type: DataType.ANY,
  2040. validate: 'myValidator',
  2041. },
  2042. },
  2043. });
  2044. S.getService(ModelDataValidator).validate('model', {
  2045. foo: 'test',
  2046. });
  2047. });
  2048. it('throws an error if the validator returns a promise', function () {
  2049. const S = new Schema();
  2050. S.getService(PropertyValidatorRegistry).addValidator(
  2051. 'myValidator',
  2052. () => Promise.resolve(true),
  2053. );
  2054. S.defineModel({
  2055. name: 'model',
  2056. properties: {
  2057. foo: {
  2058. type: DataType.ANY,
  2059. validate: 'myValidator',
  2060. },
  2061. },
  2062. });
  2063. const throwable = () =>
  2064. S.getService(ModelDataValidator).validate('model', {
  2065. foo: 'test',
  2066. });
  2067. expect(throwable).to.throw(
  2068. 'Asynchronous property validators are not supported, ' +
  2069. 'but the property validator "myValidator" returns a Promise.',
  2070. );
  2071. });
  2072. it('throws an error for non-true result from the validator', function () {
  2073. const testFn = v => {
  2074. const S = new Schema();
  2075. S.getService(PropertyValidatorRegistry).addValidator(
  2076. 'myValidator',
  2077. () => v,
  2078. );
  2079. S.defineModel({
  2080. name: 'model',
  2081. properties: {
  2082. foo: {
  2083. type: DataType.ANY,
  2084. validate: 'myValidator',
  2085. },
  2086. },
  2087. });
  2088. const throwable = () =>
  2089. S.getService(ModelDataValidator).validate('model', {
  2090. foo: 'test',
  2091. });
  2092. expect(throwable).to.throw(
  2093. 'The property "foo" of the model "model" has an invalid value "test" ' +
  2094. 'that caught by the validator "myValidator".',
  2095. );
  2096. };
  2097. testFn('str');
  2098. testFn('');
  2099. testFn(10);
  2100. testFn(0);
  2101. testFn(false);
  2102. testFn(undefined);
  2103. testFn(null);
  2104. testFn({});
  2105. testFn([]);
  2106. testFn(() => undefined);
  2107. });
  2108. it('passes arguments to the validator', function () {
  2109. let validated = false;
  2110. const S = new Schema();
  2111. const myValidator = function (value, options, context) {
  2112. expect(value).to.be.eq('test');
  2113. expect(options).to.be.undefined;
  2114. expect(context).to.be.eql({
  2115. validatorName: 'myValidator',
  2116. modelName: 'model',
  2117. propName: 'foo',
  2118. });
  2119. validated = true;
  2120. return true;
  2121. };
  2122. S.getService(PropertyValidatorRegistry).addValidator(
  2123. 'myValidator',
  2124. myValidator,
  2125. );
  2126. S.defineModel({
  2127. name: 'model',
  2128. properties: {
  2129. foo: {
  2130. type: DataType.ANY,
  2131. validate: 'myValidator',
  2132. },
  2133. },
  2134. });
  2135. S.getService(ModelDataValidator).validate('model', {
  2136. foo: 'test',
  2137. });
  2138. expect(validated).to.be.true;
  2139. });
  2140. it('invokes the validator only once per value', function () {
  2141. let invoked = 0;
  2142. const myValidator = function () {
  2143. invoked++;
  2144. return true;
  2145. };
  2146. const S = new Schema();
  2147. S.getService(PropertyValidatorRegistry).addValidator(
  2148. 'myValidator',
  2149. myValidator,
  2150. );
  2151. S.defineModel({
  2152. name: 'model',
  2153. properties: {
  2154. foo: {
  2155. type: DataType.ANY,
  2156. validate: 'myValidator',
  2157. },
  2158. },
  2159. });
  2160. S.getService(ModelDataValidator).validate('model', {
  2161. foo: 'test',
  2162. });
  2163. expect(invoked).to.be.eq(1);
  2164. });
  2165. });
  2166. describe('the option "validate" with an array value', function () {
  2167. it('does nothing for an empty array validators', function () {
  2168. const S = new Schema();
  2169. S.defineModel({
  2170. name: 'model',
  2171. properties: {
  2172. foo: {
  2173. type: DataType.ANY,
  2174. validate: [],
  2175. },
  2176. },
  2177. });
  2178. S.getService(ModelDataValidator).validate('model', {
  2179. foo: 'test',
  2180. });
  2181. });
  2182. it('does not validate a property value if it is not provided', function () {
  2183. const S = new Schema();
  2184. S.getService(PropertyValidatorRegistry).addValidator(
  2185. 'myValidator',
  2186. () => false,
  2187. );
  2188. S.defineModel({
  2189. name: 'model',
  2190. properties: {
  2191. foo: {
  2192. type: DataType.ANY,
  2193. validate: ['myValidator'],
  2194. },
  2195. },
  2196. });
  2197. const validator = S.getService(ModelDataValidator);
  2198. validator.validate('model', {});
  2199. });
  2200. it('does not validate undefined and null values', function () {
  2201. const S = new Schema();
  2202. S.getService(PropertyValidatorRegistry).addValidator(
  2203. 'myValidator',
  2204. () => false,
  2205. );
  2206. S.defineModel({
  2207. name: 'model',
  2208. properties: {
  2209. foo: {
  2210. type: DataType.ANY,
  2211. validate: ['myValidator'],
  2212. },
  2213. },
  2214. });
  2215. const validator = S.getService(ModelDataValidator);
  2216. validator.validate('model', {foo: undefined});
  2217. validator.validate('model', {foo: null});
  2218. });
  2219. it('throws an error from the validator', function () {
  2220. const myValidator = function () {
  2221. throw Error('My error');
  2222. };
  2223. const S = new Schema();
  2224. S.getService(PropertyValidatorRegistry).addValidator(
  2225. 'myValidator',
  2226. myValidator,
  2227. );
  2228. S.defineModel({
  2229. name: 'model',
  2230. properties: {
  2231. foo: {
  2232. type: DataType.ANY,
  2233. validate: ['myValidator'],
  2234. },
  2235. },
  2236. });
  2237. const throwable = () =>
  2238. S.getService(ModelDataValidator).validate('model', {
  2239. foo: 'test',
  2240. });
  2241. expect(throwable).to.throw('My error');
  2242. });
  2243. it('allows the given value if validators returns true', function () {
  2244. const S = new Schema();
  2245. S.getService(PropertyValidatorRegistry)
  2246. .addValidator('myValidator1', () => true)
  2247. .addValidator('myValidator2', () => true);
  2248. S.defineModel({
  2249. name: 'model',
  2250. properties: {
  2251. foo: {
  2252. type: DataType.ANY,
  2253. validate: ['myValidator1', 'myValidator2'],
  2254. },
  2255. },
  2256. });
  2257. S.getService(ModelDataValidator).validate('model', {
  2258. foo: 'test',
  2259. });
  2260. });
  2261. it('throws an error if the validator returns a promise', function () {
  2262. const S = new Schema();
  2263. S.getService(PropertyValidatorRegistry).addValidator(
  2264. 'myValidator',
  2265. () => Promise.resolve(true),
  2266. );
  2267. S.defineModel({
  2268. name: 'model',
  2269. properties: {
  2270. foo: {
  2271. type: DataType.ANY,
  2272. validate: ['myValidator'],
  2273. },
  2274. },
  2275. });
  2276. const throwable = () =>
  2277. S.getService(ModelDataValidator).validate('model', {
  2278. foo: 'test',
  2279. });
  2280. expect(throwable).to.throw(
  2281. 'Asynchronous property validators are not supported, ' +
  2282. 'but the property validator "myValidator" returns a Promise.',
  2283. );
  2284. });
  2285. it('throws an error by non-true result from one of validators', function () {
  2286. const testFn = v => {
  2287. const S = new Schema();
  2288. S.getService(PropertyValidatorRegistry)
  2289. .addValidator('myValidator1', () => true)
  2290. .addValidator('myValidator2', () => v);
  2291. S.defineModel({
  2292. name: 'model',
  2293. properties: {
  2294. foo: {
  2295. type: DataType.ANY,
  2296. validate: ['myValidator1', 'myValidator2'],
  2297. },
  2298. },
  2299. });
  2300. const throwable = () =>
  2301. S.getService(ModelDataValidator).validate('model', {
  2302. foo: 'test',
  2303. });
  2304. expect(throwable).to.throw(
  2305. 'The property "foo" of the model "model" has an invalid value "test" ' +
  2306. 'that caught by the validator "myValidator2".',
  2307. );
  2308. };
  2309. testFn('str');
  2310. testFn('');
  2311. testFn(10);
  2312. testFn(0);
  2313. testFn(false);
  2314. testFn(undefined);
  2315. testFn(null);
  2316. testFn({});
  2317. testFn([]);
  2318. testFn(() => undefined);
  2319. });
  2320. it('passes arguments to the validator', function () {
  2321. let validated = false;
  2322. const S = new Schema();
  2323. const myValidator = function (value, options, context) {
  2324. expect(value).to.be.eq('test');
  2325. expect(options).to.be.undefined;
  2326. expect(context).to.be.eql({
  2327. validatorName: 'myValidator',
  2328. modelName: 'model',
  2329. propName: 'foo',
  2330. });
  2331. validated = true;
  2332. return true;
  2333. };
  2334. S.getService(PropertyValidatorRegistry).addValidator(
  2335. 'myValidator',
  2336. myValidator,
  2337. );
  2338. S.defineModel({
  2339. name: 'model',
  2340. properties: {
  2341. foo: {
  2342. type: DataType.ANY,
  2343. validate: ['myValidator'],
  2344. },
  2345. },
  2346. });
  2347. S.getService(ModelDataValidator).validate('model', {
  2348. foo: 'test',
  2349. });
  2350. expect(validated).to.be.true;
  2351. });
  2352. it('invokes validators by the given order', function () {
  2353. const invocation = [];
  2354. const validator1 = function () {
  2355. invocation.push('myValidator1');
  2356. return true;
  2357. };
  2358. const validator2 = function () {
  2359. invocation.push('myValidator2');
  2360. return true;
  2361. };
  2362. const S = new Schema();
  2363. S.getService(PropertyValidatorRegistry)
  2364. .addValidator('myValidator1', validator1)
  2365. .addValidator('myValidator2', validator2);
  2366. S.defineModel({
  2367. name: 'model',
  2368. properties: {
  2369. foo: {
  2370. type: DataType.ANY,
  2371. validate: ['myValidator1', 'myValidator2'],
  2372. },
  2373. },
  2374. });
  2375. S.getService(ModelDataValidator).validate('model', {
  2376. foo: 'test',
  2377. });
  2378. expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
  2379. });
  2380. });
  2381. describe('the option "validate" with an object value', function () {
  2382. it('does nothing for an empty validators object', function () {
  2383. const S = new Schema();
  2384. S.defineModel({
  2385. name: 'model',
  2386. properties: {
  2387. foo: {
  2388. type: DataType.ANY,
  2389. validate: {},
  2390. },
  2391. },
  2392. });
  2393. S.getService(ModelDataValidator).validate('model', {
  2394. foo: 'test',
  2395. });
  2396. });
  2397. it('does not validate a property value if it is not provided', function () {
  2398. const S = new Schema();
  2399. S.getService(PropertyValidatorRegistry).addValidator(
  2400. 'myValidator',
  2401. () => false,
  2402. );
  2403. S.defineModel({
  2404. name: 'model',
  2405. properties: {
  2406. foo: {
  2407. type: DataType.ANY,
  2408. validate: {
  2409. myValidator: true,
  2410. },
  2411. },
  2412. },
  2413. });
  2414. const validator = S.getService(ModelDataValidator);
  2415. validator.validate('model', {});
  2416. });
  2417. it('does not validate undefined and null values', function () {
  2418. const S = new Schema();
  2419. S.getService(PropertyValidatorRegistry).addValidator(
  2420. 'myValidator',
  2421. () => false,
  2422. );
  2423. S.defineModel({
  2424. name: 'model',
  2425. properties: {
  2426. foo: {
  2427. type: DataType.ANY,
  2428. validate: {
  2429. myValidator: true,
  2430. },
  2431. },
  2432. },
  2433. });
  2434. const validator = S.getService(ModelDataValidator);
  2435. validator.validate('model', {foo: undefined});
  2436. validator.validate('model', {foo: null});
  2437. });
  2438. it('throws an error from the validator', function () {
  2439. const myValidator = function () {
  2440. throw Error('My error');
  2441. };
  2442. const S = new Schema();
  2443. S.getService(PropertyValidatorRegistry).addValidator(
  2444. 'myValidator',
  2445. myValidator,
  2446. );
  2447. S.defineModel({
  2448. name: 'model',
  2449. properties: {
  2450. foo: {
  2451. type: DataType.ANY,
  2452. validate: {
  2453. myValidator: true,
  2454. },
  2455. },
  2456. },
  2457. });
  2458. const throwable = () =>
  2459. S.getService(ModelDataValidator).validate('model', {
  2460. foo: 'test',
  2461. });
  2462. expect(throwable).to.throw('My error');
  2463. });
  2464. it('allows the given value if validators returns true', function () {
  2465. const S = new Schema();
  2466. S.getService(PropertyValidatorRegistry)
  2467. .addValidator('myValidator1', () => true)
  2468. .addValidator('myValidator2', () => true);
  2469. S.defineModel({
  2470. name: 'model',
  2471. properties: {
  2472. foo: {
  2473. type: DataType.ANY,
  2474. validate: {
  2475. myValidator1: true,
  2476. myValidator2: true,
  2477. },
  2478. },
  2479. },
  2480. });
  2481. S.getService(ModelDataValidator).validate('model', {
  2482. foo: 'test',
  2483. });
  2484. });
  2485. it('throws an error if the validator returns a promise', function () {
  2486. const S = new Schema();
  2487. S.getService(PropertyValidatorRegistry).addValidator(
  2488. 'myValidator',
  2489. () => Promise.resolve(true),
  2490. );
  2491. S.defineModel({
  2492. name: 'model',
  2493. properties: {
  2494. foo: {
  2495. type: DataType.ANY,
  2496. validate: {
  2497. myValidator: true,
  2498. },
  2499. },
  2500. },
  2501. });
  2502. const throwable = () =>
  2503. S.getService(ModelDataValidator).validate('model', {
  2504. foo: 'test',
  2505. });
  2506. expect(throwable).to.throw(
  2507. 'Asynchronous property validators are not supported, ' +
  2508. 'but the property validator "myValidator" returns a Promise.',
  2509. );
  2510. });
  2511. it('throws an error by non-true result from one of validators', function () {
  2512. const testFn = v => {
  2513. const S = new Schema();
  2514. S.getService(PropertyValidatorRegistry)
  2515. .addValidator('myValidator1', () => true)
  2516. .addValidator('myValidator2', () => v);
  2517. S.defineModel({
  2518. name: 'model',
  2519. properties: {
  2520. foo: {
  2521. type: DataType.ANY,
  2522. validate: {
  2523. myValidator1: true,
  2524. myValidator2: true,
  2525. },
  2526. },
  2527. },
  2528. });
  2529. const throwable = () =>
  2530. S.getService(ModelDataValidator).validate('model', {
  2531. foo: 'test',
  2532. });
  2533. expect(throwable).to.throw(
  2534. 'The property "foo" of the model "model" has an invalid value "test" ' +
  2535. 'that caught by the validator "myValidator2".',
  2536. );
  2537. };
  2538. testFn('str');
  2539. testFn('');
  2540. testFn(10);
  2541. testFn(0);
  2542. testFn(false);
  2543. testFn(undefined);
  2544. testFn(null);
  2545. testFn({});
  2546. testFn([]);
  2547. testFn(() => undefined);
  2548. });
  2549. it('passes arguments to the validator', function () {
  2550. let validated = false;
  2551. const S = new Schema();
  2552. const myValidator = function (value, options, context) {
  2553. expect(value).to.be.eq('test');
  2554. expect(options).to.be.eql({
  2555. option1: 'value1',
  2556. option2: 'value2',
  2557. });
  2558. expect(context).to.be.eql({
  2559. validatorName: 'myValidator',
  2560. modelName: 'model',
  2561. propName: 'foo',
  2562. });
  2563. validated = true;
  2564. return true;
  2565. };
  2566. S.getService(PropertyValidatorRegistry).addValidator(
  2567. 'myValidator',
  2568. myValidator,
  2569. );
  2570. S.defineModel({
  2571. name: 'model',
  2572. properties: {
  2573. foo: {
  2574. type: DataType.ANY,
  2575. validate: {
  2576. myValidator: {
  2577. option1: 'value1',
  2578. option2: 'value2',
  2579. },
  2580. },
  2581. },
  2582. },
  2583. });
  2584. S.getService(ModelDataValidator).validate('model', {
  2585. foo: 'test',
  2586. });
  2587. expect(validated).to.be.true;
  2588. });
  2589. it('invokes validators by the given order', function () {
  2590. const invocation = [];
  2591. const validator1 = function () {
  2592. invocation.push('myValidator1');
  2593. return true;
  2594. };
  2595. const validator2 = function () {
  2596. invocation.push('myValidator2');
  2597. return true;
  2598. };
  2599. const S = new Schema();
  2600. S.getService(PropertyValidatorRegistry)
  2601. .addValidator('myValidator1', validator1)
  2602. .addValidator('myValidator2', validator2);
  2603. S.defineModel({
  2604. name: 'model',
  2605. properties: {
  2606. foo: {
  2607. type: DataType.ANY,
  2608. validate: {
  2609. myValidator1: true,
  2610. myValidator2: true,
  2611. },
  2612. },
  2613. },
  2614. });
  2615. S.getService(ModelDataValidator).validate('model', {
  2616. foo: 'test',
  2617. });
  2618. expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
  2619. });
  2620. it('validates even the validator options is false', function () {
  2621. let validated = false;
  2622. const myValidator = function () {
  2623. validated = true;
  2624. return true;
  2625. };
  2626. const S = new Schema();
  2627. S.getService(PropertyValidatorRegistry).addValidator(
  2628. 'myValidator',
  2629. myValidator,
  2630. );
  2631. S.defineModel({
  2632. name: 'model',
  2633. properties: {
  2634. foo: {
  2635. type: DataType.ANY,
  2636. validate: {
  2637. myValidator: false,
  2638. },
  2639. },
  2640. },
  2641. });
  2642. S.getService(ModelDataValidator).validate('model', {
  2643. foo: 'test',
  2644. });
  2645. expect(validated).to.be.true;
  2646. });
  2647. });
  2648. it('the option "validate" requires a non-empty String, an Array or an Object', function () {
  2649. const schema = new Schema();
  2650. schema
  2651. .getService(PropertyValidatorRegistry)
  2652. .addValidator('myValidator', () => true);
  2653. schema.defineModel({
  2654. name: 'model',
  2655. properties: {
  2656. foo: {
  2657. type: DataType.ANY,
  2658. validate: undefined,
  2659. },
  2660. },
  2661. });
  2662. const V = schema.getService(ModelDataValidator);
  2663. const throwable = v => () => {
  2664. const models = schema.getService(DefinitionRegistry)['_models'];
  2665. models.model.properties.foo.validate = v;
  2666. V.validate('model', {foo: 'bar'});
  2667. };
  2668. const error = v =>
  2669. format(
  2670. 'The provided option "validate" of the property "foo" in the model "model" ' +
  2671. 'should be a non-empty String, an Array of String or an Object, ' +
  2672. 'but %s given.',
  2673. v,
  2674. );
  2675. expect(throwable('')).to.throw(error('""'));
  2676. expect(throwable(10)).to.throw(error('10'));
  2677. expect(throwable(0)).to.throw(error('0'));
  2678. expect(throwable(true)).to.throw(error('true'));
  2679. expect(throwable(false)).to.throw(error('false'));
  2680. throwable('myValidator')();
  2681. throwable(['myValidator'])();
  2682. throwable([])();
  2683. throwable({myValidator: true})();
  2684. throwable({})();
  2685. });
  2686. });
  2687. });
  2688. });